File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets 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: 19649
    
  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
 
 
subject: java.util.ConcurrentModificationException
 
Similar Threads
Anonymous inner class
JAVA BATTLESHIP HELP ! PLEASE
I am new to Java programming
Array overwritten
Calling seperate functions