File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterator" Watch "Iterator" New topic
Author

Iterator

Dan Silva
Ranch Hand

Joined: Sep 05, 2007
Posts: 86
My buddy told me (but wasn't positive about it) that you can substitute a 'for each' loop wherever you would use the Iterator class. Is this true? If it's not, then could someone explain the purpose and use of the Iterator class? Thanks so much.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The enhanced for statement ("for-each") can be used either with an array or with any container that implements the Iterable interface. (See JLS 14.14.2 The enhanced for statement.) In fact, for Iterable containers, the for-each loop uses an Iterator behind the scenes.

But I wouldn't say that a for-each loop can be "substituted" wherever an Iterator is used, because an Iterator usually has more functionality than what's required in a for-each loop. For an idea of what else an Iterator might do, see the API documentation for Iterator and ListIterator.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Dan - This link includes some information at the bottom on where the for-each loop cannot be substituted for an iterator -
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jinny Morris:
...This link...

Let's try the internet version instead of your local C-drive.
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Ooooops!


Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by marc weber:
The enhanced for statement ("for-each") can be used either with an array or with any container that implements the Iterable interface. (See JLS 14.14.2 The enhanced for statement.)


Interestingly, Iterator does not implement Iterable. So if you have an iterator, you can't use it in a for-each loop.

- Adam
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

An Iterable object has an iterator() method, which returns an Iterator.

In general, an Iterable "contains" the elements to be iterated over, and is able to provide an Iterator to perform the iterating.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Right. As a consequence, you rarely if ever want to pass around an Iterator, because you can't easily get an Iterable from it. Well you can do it certainly, but it's not in a standard library:

Which is an annoying bit of boilerplate to have to add on. Sigh. Anyway, the result is that it's usually easier to pass around the Collection that you get the Iterator from, rather than the actual Iterator.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterator