首先考虑下面代码的结果
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(i);
}
for (Integer integer : list) {
System.out.println(integer);
if(integer.equals(0)){
list.remove(integer);
}
}
System.out.println(list);
会报错
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1043)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:997)
at Demo1.main(Demo1.java:13)
在来看这一段代码
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(i);
}
for (Integer integer : list) {
System.out.