Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ListIterator to ArrayList ?  RSS feed

 
kri shan
Ranch Hand
Posts: 1487
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I converted ArrayList to ListIterator and did some operation(remove() ) on the list. Again i want to convert ListIterator to ArrayList ?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36406
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kri,
You could go through the iterator and add each element to a new ArrayList. Some iterators affect the underlying collection. I don't remember if ListIterator is one of them.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, most (List)Iterators modify the underlyng collection when calling remove, add or set. I have yet to encounter the first implementation that doesn't - or the methods throw an exception.

As for adding the iterator's elements to a new ArrayList: you first have to "reset" the iterator. Fortunately, that is possible with ListIterators:

That is a trick you can never do with a regular Iterator - once you'ver reached the end, there's no going back.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!