【发布时间】:2014-03-11 06:05:44
【问题描述】:
当从foreach 循环内部删除ArrayList 的元素时。它打破了循环。
抛出 ConcurrentModificationException
我的问题是为什么它会打破循环。
代码。
ArrayList gStudyLst=getLst();
int recordCount=0;
for(String study : gStudyLst){
if(++recordCount < 10){
if(generateFile(study)){
gStudyLst.remove(study); // there its break throw ConcurrentModificationException
}else{
System.out.println("File not generated"):
gStudyLst.clear();
return false;
}
}
}else{
return true;
}
}
【问题讨论】:
-
是的,您需要使用 Iterator.remove() 或索引删除。