帮楼主仔细检查了一遍,原来是第19行ok写成0k了!!
import java.util.*;
class YiChang {
public static void sop(Object obj) {
System.out.println(obj);
}
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add("ok1");
al.add("ok2");
al.add("ok3");
sop(al);
Iterator it = al.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj.equals("ok2")) {
al.add("ok4");
it.remove();
}
sop(obj);
}
sop(al);
}
}
你要的报错结果如下:
[ok1, ok2, ok3]
ok1
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.remove(Unknown Source)
at YiChang.main(YiChang.java:19)
|