Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ListIterator to ArrayList ?

 
kri shan
Ranch Hand
Posts: 1478
  • 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
Marshal
Posts: 34973
379
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
Pie
Posts: 20667
65
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