could somebody explain what Iterator and ListIterator does? when should we use Iterator and when ListIterator? " ...ListIterator allows you to iterate backwards over a list as well as forwards." How is the iterate backwards working? could someone give me a simple example? and a question about the example code on page 388( Thinking in Java), after get the iterator and print out, "the order in which the elements are listed is not the order in which they were added." why is that? the iterator can do the sorting as well? thanks in advance,
I'll give this a shot with a simple example. I hardly ever use an iterator, because I didn't quite understand Iterator also for a very long time, and am slowly trying to work them in.
I think you can understand what's happening in main(). When I do Iterator i = l.iterator() ; I'm creating an Iterator that holds the elements of this List. I can now use this iterator to easily cycle through my List, without having to write all the code out myself. hasNext() is used to test if there is another element, and next() returns the next Object in the iterator, so that you can do whatever you want to it (you might have to some casting here. Since I am using Strings, I don't really need to).
As far as the order, the API for Collections states
public Iterator iterator()
Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).
I could be wrong about this, but I think ArrayList provides this guarantee, because it always seems to iterate through the list from element 0 on up. Someone correct me here if I am mistaken, I'm going off of my own experience.
ListIterator is a subclass of Iterator, and provides more functionality. The API states
An iterator for lists that allows the programmer to traverse the list in either direction and modify the list during iteration.
I would guess you would use this anytime you don't want to just cycle through the list, but instead want to bounce around depending on certain criteria, and make needed changes as you go. Never used it, but after looking it over, it appears quite handy to use.
Biggest suggestion is read the API. I wouldn't worry about how it is being implemented (I used to do that myself, can drive you batty), and rather focus on how you would use what is already laid down before you.
Hope this helps some, Jason
[This message has been edited by jason adam (edited September 07, 2001).]
[This message has been edited by Marilyn deQueiroz (edited September 07, 2001).]
Originally posted by jason adam: I could be wrong about this, but I think ArrayList provides this guarantee, because it always seems to iterate through the list from element 0 on up. Someone correct me here if I am mistaken
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Chicken Farmer ()
Joined: May 08, 2001
So why does it always seem to return the list in the correct order (by correct order I mean from 0 to length - 1)? I'm not using really huge ArrayLists, that might be why. The way I read it, the iterator gets the list, but the elements are going to be in a random-like order. As Lance mentioned, iterators are resource intensive. Are they used a lot in the real-world, or is writing your own logic and loops more prevalent? Thanks for the clarification Marilyn, and sorry about the extra code Jason [This message has been edited by jason adam (edited September 07, 2001).]
Marilyn de Queiroz
Joined: Jul 22, 2000
The iterator steps through the list in the order of the list from the first to the last, but the elements of the list are not necessarily in order. LIST ----- g h a c r prints g h a c r sorted = a c g h r