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

后端 未结 10 2120
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  -上瘾入骨i
    2020-11-22 08:47

    Change Iterator for each into for loop to solve.

    And the Reason is:

    The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

    --Referred Java Docs.

提交回复
热议问题