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

后端 未结 5 1697
余生分开走
余生分开走 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:24

    You cannot modify a List while you are iterating over it which you are doing in the first example. In the second you simply have a regular for loop.

提交回复
热议问题