List<T> list = list 赋值是引用,不是值
public static void main(String[] args) { ArrayList<String> listOld = new ArrayList<String>(); listOld.add("a"); listOld.add("b"); listOld.add("c"); listOld.add("d"); while(listOld.size()>0){ ArrayList<String> listNew = listOld; listNew.remove(0); System.out.println(listNew.toString()); } }
输出:
[b, c, d]
[c, d]
[d]
[]
public static void main(String[] args) { ArrayList<String> listOld = new ArrayList<String>(); listOld.add("a"); listOld.add("b"); listOld.add("c"); listOld.add("d"); while(listOld.size()>0){ ArrayList<String> listNew = new ArrayList<String>(); listNew.addAll(listOld); listNew.remove(0); System.out.println(listNew.toString()); } }
输出:
[b, c, d]
[b, c, d]
............
死循环