Jan
30
|
The general cause of a ConcurrentModificationException is from trying to remove an instance from a collection that is being iterated over as in the following example.
Iterator i = mylist.iterator(); while (i.hasNext()) { Object o = i.next(); if (something) { // following line will throw ConcurrentModificationException mylist.remove(o); } }
The correct way is to instead use the Iterator’s remove() method as shown in the following snippet:
Iterator i = mylist.iterator(); while (i.hasNext()) { Object o = i.next(); if (something) { i.remove(); } }
Array ( ) 6 Responses to “How to fix ConcurrentModificationException?”
Leave a Reply
You must be logged in to post a comment.
May 27th, 2011 at 4:16 am
Thank you very much! This solved a problem I had that was driving me crazy.
June 21st, 2011 at 6:54 pm
Thank you, you just save my day !!!
August 6th, 2011 at 5:03 am
Like the others, thanks, you’ve saved me a headache!
August 30th, 2011 at 3:01 am
Thanks for the explanation!!!
In my case I decided to do clone(), so to avoid this, because my program runs in two threads.
January 13th, 2012 at 5:12 pm
Thanks a lot…
February 27th, 2012 at 8:07 pm
Thx, worked like a charm 🙂