Why does one loop throw a ConcurrentModificationException, while the other doesn't?

后端 未结 5 1667
余生分开走
余生分开走 2020-12-16 18:40

I\'ve run into this while writing a Traveling Salesman program. For an inner loop, I tried a

for(Point x:ArrayList) {
// modify the iterator
}         


        
5条回答
  •  时光说笑
    2020-12-16 19:29

    the first code is using an iterator so modifying the collection is not allowed. The second code you are accessing each object with x.get(i), so not using an iterator, modifications thus are allowed

提交回复
热议问题