wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ListIterator to ArrayList ?" Watch "ListIterator to ArrayList ?" New topic
Author

ListIterator to ArrayList ?

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
I converted ArrayList to ListIterator and did some operation(remove() ) on the list. Again i want to convert ListIterator to ArrayList ?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: ListIterator to ArrayList ?
 
Similar Threads
Collections which iterate bi-directionally?
Ordering in ArrayList
Collection
Remove Button and Concurrent modification exception
retrieving elements from a vector in reverse order