• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterator

 
Dan Silva
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jinny Morris
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan - This link includes some information at the bottom on where the for-each loop cannot be substituted for an iterator -
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jinny Morris:
...This link...

Let's try the internet version instead of your local C-drive.
 
Jinny Morris
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooooops!


 
Adam Nace
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic