Hi
I have an arraylist and associated with it 2 iterators. When iterating through the arraylist in the forward and reverse direction and removing nodes under certain criteria. I get a concurrent modification error. This is with using the iterators remove() method and not the arraylist remove method. The only workaround for this is to "mark" the nodes and then remove them after the iteration. Is there a way to do this during the iteration process?
Cheers,
Sharp Tool
NullBock - 22 Jan 2006 12:47 GMT
The problem is the *two* iterators. You may not use an iterator *and*
modify a list in any other way--in your case, with a second iterator.
You shouldn't have any problems at all iterating through a list and
altering it, so long as you are using only one iterator at a time.
Walter Gildersleeve
Freiburg, Germany
______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
Adam Maass - 23 Jan 2006 00:40 GMT
>Hi
>
[quoted text clipped - 6 lines]
>workaround for this is to "mark" the nodes and then remove them after the
>iteration. Is there a way to do this during the iteration process?
As has been pointed out, ConcurrentModificationException will arise if the
backing ArrayList of one iterator is modified in any way outside of the
"remove" method of that iterator -- including the "remove" method of a
second iterator on the same ArrayList. This is expected and is as
documented.
It might be possible to build up a second list of elements to keep while
iterating through your first ArrayList, then replace the original.
-- Adam Maass