Java 引用传值之向List<List<...>>中插值
public class Test { public static void main(String[] args) { List<List<Integer>> listList = new ArrayList<List<Integer>>(); List<Integer> l = new ArrayList<Integer>(); for (int i = 0;i<3;i++){ l.add(i); } listList.add(l); for (int i=0;i<3;i++){ l.remove(new Integer(i)); } for (List<Integer> list : listList) { for (Integer integer : list) { System.out.printf(integer+" "); } System.out.println(); } } }
执行上述代码,结果为空。因为new出来的的对象在堆上,listList中保存的是对象的引用,在外部修改会导致堆上的对象内容被修改。

浙公网安备 33010602011771号