aspose file tools*
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 Spring in Action this week in the Spring forum!
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: 1378
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

Joined: May 26, 2003
Posts: 30758
    
156

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: 19719
    
  20

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ListIterator to ArrayList ?