problem with ListIterator

L

Lucia

Hello,

I've read about the listiterator. It allows me to traverse the list in
either direction and modify the list during iteration.

But I've tried to add list elements during the iteration and got the
error message.
-- java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:444)
at java.util.AbstractList$Itr.next(AbstractList.java:421)

Is it possible to add the new elements at the end of the list??


Thanks

Lucia
 
S

Steve W. Jackson

:Hello,
:
:I've read about the listiterator. It allows me to traverse the list in
:either direction and modify the list during iteration.
:
:But I've tried to add list elements during the iteration and got the
:error message.
:-- java.util.ConcurrentModificationException
:at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:444)
:at java.util.AbstractList$Itr.next(AbstractList.java:421)
:
:Is it possible to add the new elements at the end of the list??
:
:
:Thanks
:
:Lucia

The API for the ListIterator says it lets you modify the list, but
you'll note that the add(Object o) method is also designated as
optional, which means that the implementation returned by any specific
list may not support that operation.

However, in my experience the ConcurrentModificationException has always
resulted from attempts to modify the list separately. That is, after
getting a ListIterator, the list itself is modified, and then a
subsequent attempt is made to use the iterator. You need to be certain
that the List which provided the iterator to you supports the add
operation, and that you don't modify that list directly while the
iterator is in use. In the case of the ListIterator specifically, it
seems (based on the API's wording) that you could take note of the
nextIndex() or previousIndex() values, modify the List, then get a new
ListIterator and quickly speed through it to regain your position.

= Steve =
 
A

Alexander Bollaert

Lucia said:
Hello,

I've read about the listiterator. It allows me to traverse the list in
either direction and modify the list during iteration.

But I've tried to add list elements during the iteration and got the
error message.
-- java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:444)
at java.util.AbstractList$Itr.next(AbstractList.java:421)

Is it possible to add the new elements at the end of the list??


Thanks

Lucia

I can't really tell what you are trying to do (you did not include the
code that does the additions), but I think you are using the List object
to add the items. If you add things to the List while you are iterating,
the Iterator will check if the List has been modified without it
knowing about it, and if the list has been changed, it causes a
ConcurrentModificationException to be thrown.

When doing an addition while iterating, you have to use the ListIterator
object, and not the List object you are iterating over. So instead of
doing calling List#add(), you should call ListIterator#add(), using the
iterator instead of the list.

Hopes this helps,

Alex
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top