problem with ListIterator  
Author Message
cheng_lucia





PostPosted: 2003-10-31 3:09:00 Top

java-programmer, problem with ListIterator 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
 
Steve W. Jackson





PostPosted: 2003-10-31 3:41:00 Top

java-programmer >> problem with ListIterator In article <email***@***.com>,
email***@***.com (Lucia) wrote:

>: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 =
--
Steve W. Jackson
Montgomery, Alabama
 
Alexander Bollaert





PostPosted: 2003-10-31 3:45:00 Top

java-programmer >> problem with ListIterator Lucia wrote:
> 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


 
 
cheng_lucia





PostPosted: 2003-11-2 21:56:00 Top

java-programmer >> problem with ListIterator Hi, Steve and Alex,

thanks, and it works.

best regards

lucia