Meaningless Drivel is fun!*
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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: 19684
    
  20

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
 
wood burning stoves
 
subject: java.util.ConcurrentModificationException