This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ListIterator to ArrayList ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ListIterator to ArrayList ?" Watch "ListIterator to ArrayList ?" New topic

ListIterator to ArrayList ?

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1400
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

Joined: May 26, 2003
Posts: 32649

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Rob Spoor

Joined: Oct 27, 2005
Posts: 20137

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: ListIterator to ArrayList ?
It's not a secret anymore!