aspose file tools*
The moose likes Java in General and the fly likes Removing from a ArrayList while iterating over it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Removing from a ArrayList while iterating over it" Watch "Removing from a ArrayList while iterating over it" New topic
Author

Removing from a ArrayList while iterating over it

Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 77
Hello,

I am trying to remove some of the ArrayList during iteration over it. If I call the remove on one of the members I receive "java.util.AbstractList$Itr.checkForComodification" exception.

To avoid this I have added a line of code to repair the iterator (in 2nd code). Is this the right way to achieve my purpose or I should do something else.

Thank you,



Corrected:
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
No, you should use the remove() method of Iterator.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 77
Thank you very much.
Spoon Factorial
Greenhorn

Joined: Dec 23, 2007
Posts: 1
if you need to remove from it often, you should use a LinkedList instead of an ArrayList
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

"Spoon Factorial", please check your private messages.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing from a ArrayList while iterating over it
 
Similar Threads
jsp question from "using jsp" chapter of Head First book
Regarding Generics usage in jdk1.5
Removing Items from an Iterated ArrayList
Iterator add and it.remove();
ArrayList in a tester class