This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes java.util.ConcurrentModificationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.util.ConcurrentModificationException" Watch "java.util.ConcurrentModificationException" New topic
Author

java.util.ConcurrentModificationException

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Hi all. Trying to manipulate an ArrayList and i get this error, what do i do?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

When you iterate over most collections (either using an explicit Iterator or by using the for-each loop), any method call that modifies the collection will throw this exception. There are only two solutions:
1) use an explicit iterator and use the iterator's remove() method.
2) for lists only, loop using indexing. Be aware that when you delete an element, the indexing will also change. It's usually safest to loop backwards:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.util.ConcurrentModificationException