我在编写旅行商问题程序时遇到了这个问题。 对于内部循环,我尝试使用
。for(Point x:ArrayList<Point>) {
// modify the iterator
}
但是,当我将另一个点添加到该列表中时,会导致抛出ConcurrentModificationException
异常。
然而,当我将循环改为
for(int x=0; x<ArrayList<Point>.size(); x++) {
// modify the array
}
循环正常运行且未引发异常。
两个循环都是for循环,为什么一个会引发异常而另一个不会呢?