This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes can' t understand iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "can Watch "can New topic
Author

can' t understand iterator

christie xu
Greenhorn

Joined: Mar 06, 2001
Posts: 11
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,
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
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
public interface ListIterator
extends Iterator

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).]
Lance Finney
Ranch Hand

Joined: Apr 26, 2001
Posts: 133
I've never used Iterator myself, but Peter Haggar recommends not using Iterator in "Practical Java" due to performance.
According to the book, this approach:

is about 29 to 34 percent faster than this approach:
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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
You are mistaken. If you want sorted, you have to sort() the List yourself, or use TreeSet which implements SortedSet or TreeMap which implements SortedMap.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
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
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can' t understand iterator
 
Similar Threads
constructor for a class implementing doubly-linked list
Iterator and ListIterator.....
how to read text file backwards
ArrayList can be iterated bidirectionaly!
Regarding TypeSafe Iterator