Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing from a ArrayList while iterating over it

 
Siamak Saarmann
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you should use the remove() method of Iterator.
 
Siamak Saarmann
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
 
Spoon Factorial
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you need to remove from it often, you should use a LinkedList instead of an ArrayList
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Spoon Factorial", please check your private messages.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic