我们都知道,在迭代它时从集合中删除对象的最安全的“可能只是安全”的方式是首先检索Iterator,执行循环并在需要时删除;
Iterator iter=Collection.iterator();
while(iter.hasNext()){
Object o=iter.next()
if(o.equals(what i'm looking for)){
iter.remove();
}
}
我想要理解的是,不幸的是没有发现一个深刻的技术性的解释,是这种删除是如何执行的,
如果:
for(Object o:myCollection().getObjects()){
if(o.equals(what i'm looking for)){
myCollection.remove(o);
}
}
会引发ConcurrentModificationException,“在技术术语”Iterator.remove()会做什么?它是否删除对象,打破循环并重新启动循环?
我在官方文件中看到:
“Removes the current element. Throws IllegalStateException if an
attempt is made to call remove() that is not preceded by a call to
next( ).”
部分“删除当前元素”,使我想到在“常规”循环中发生的完全相同的情况=> (执行等式测试并删除,如果需要),但为什么迭代器循环ConcurrentModification安全?