• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Why is it so

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

According to Sun Java Tutorial,

Use Iterator instead of the for-each construct when you need to:
Iterate over multiple collections in parallel.

Whats the benefit in case we go with the iterator?

Please provide your inputs on this..

Thanks,
Mohit.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The Advantage with iterator is you can get list of objects irrespective of the data structure. if you use for loop then you have to write a seperate for loop for data structure.

Do one thing ...

Search with following Key word in Google:

Iterator Design Pattern

or

http://www.google.co.in/search?hl=en&q=Iterator+Design+pattern&meta=
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that answers the question at all.

The answer is that when using "for/each", you don't actually have access to the Iterator; the loop construct looks directly at the collection or array itself. So let's say you need to do something like this:



Do you see how we're using the loop counter to access two different arrays? You can't do that with a single for/each loop; you'd have to write two loops. Therefore, don't use for/each when you need to iterate over multiple collections -- because you can't!
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are Right,

But my intention was, to make him think about the Iterator pattern so that he can get more ...
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic