Why am I not getting a java.util.ConcurrentModificationException in this example?

后端 未结 10 2117
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 07:59

Note: I am aware of the Iterator#remove() method.

In the following code sample, I don\'t understand why the List.remove in main

10条回答
  •  花落未央
    2020-11-22 08:50

    I had that same problem but in case that I was adding en element into iterated list. I made it this way

    public static void remove(Integer remove) {
        for(int i=0; i

    Now everything goes fine because you don't create any iterator over your list, you iterate over it "manually". And condition i < integerList.size() will never fool you because when you remove/add something into List size of the List decrement/increment..

    Hope it helps, for me that was solution.

提交回复
热议问题