age old question

N

newsnet customer

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
 
N

NullBock

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.
 
A

Adam Maass

newsnet customer said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top