小编use*_*569的帖子

此方法应该反转ArrayList中的数字

import java.util.*;

public class Metodo {

    public static void main(String[] args) {
        ArrayList<Integer> a = new ArrayList();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(4);
        a.add(5);
        Metodo.inverte(a);
        for(int i=0; i<a.size(); i++) {
            System.out.println(a.get(i));
        }
    }

    public static void inverte(ArrayList<Integer> a) {
        ArrayList<Integer> other = new ArrayList();
        other = a;
        for(int i=0; i<a.size(); i++) {
            a.set(i, other.get(other.size()-i-1));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此方法应该反转ArrayList中的数字,因此它应该打印"5 4 3 2 1",但它打印"5 4 3 4 5"而不是.为什么?

java

0
推荐指数
1
解决办法
58
查看次数

标签 统计

java ×1