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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

iterator method of ArrayList class

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi folk
My question is that if i can iterate an ArrayList using for loop then what does iterator() make difference here ?
 
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
In most cases, the for-each loop is the best solution compared to an iterator. But it too has its limitations. Read here to see some pros and cons of both methods:

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Netbeans IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I thought your question was about why to use iterators since we have the for loop (scratching my head......). Maybe the expression "pros and cons" that I used was not the best, but in that link you can see why and when you should use the for loop or an iterator. I don't know if that still answers your question (scratching my head again....).
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Panagiotis Kalogeropoulos wrote:I thought your question was about why to use iterators since we have the for loop (scratching my head......). Maybe the expression "pros and cons" that I used was not the best, but in that link you can see why and when you should use the for loop or an iterator. I don't know if that still answers your question (scratching my head again....).



If you really know that your that link has my answer and you are sure then why don't you post that answer of that document ?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
There are a couple of reasons:

1- Code readability. You could do this:


or you could use this:


A lot of people prefered the second option (before the enhanced for loop was around) because it was more expressive: you could read the for loop in English and it would sort of makes sense.

Of course, now you have the even better enhanced for loop which translates to the same byte code:

so the Iterator for loop is not necessary for this case anymore (its still used behind the scenes, but you just don't need to see it).

2- Portability. There is code which just needs to be able to iterate over things. It doesn't matter what those things are stored in (List, Set, Map, etc...). Most collections have an Iterator implementation, so if you just need to iterate, then you can ask for an iterator instead of a specific collection implementation:
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Oh, and
3- There are cases when you want to modify the contents of a List as you iterate over them. Using the enhanced for loop won't let you do that. You can do it with the indexed for loop, but it isn't pretty. But if you use the ListIterator then you can add, remove, or modify while iterating. (but the ListIterator isn't the same thing as Iterator)
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Netbeans IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Ok, now we are getting off track... If the link does not help you get the answer you want, then it is my bad that I did not understand correctly what you are asking. But if it does answer what you are asking then why bother if it is me that gave you the answer or a link from a webpage? Anyway, no offence, I am standing back as an observer for the responses from other ranchers who may have better and more detailed answers than mine.

EDIT: late response to

If you really know that your that link has my answer and you are sure then why don't you post that answer of that document ?

.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

saloni jhanwar wrote: My question is that if i can iterate an ArrayList using for loop then what does iterator() make difference here ?


use decompiler .... yeah you can do random access using for loop with i . but in case LinkedList you have to follow another approach[in terms of efficiency]. so the simple iterator version internally handle this .. and even better for each handle them better....
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

saloni jhanwar wrote:My question is that if i can iterate an ArrayList using for loop then what does iterator() make difference here ?


Depends what kind of for loop you're talking about. If you mean a for-each loop, then it's absolutely required, because that's based on the subject being Iterable.

If you're asking: 'Why have an iterator() if you can traverse it with a standard for loop', it has more to do with consistency. All Lists have iterators, but NOT all of them have the same structure. A LinkedList would perform very badly with a standard for loop; but it's iterator is optimally fast.

Winston
 
Marshal
Posts: 79239
377
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Closing thread because of rudeness in a post which I have deleted.
Panagiotis Kalogeropoulos, I must commend you for the dignified way you took the insult. Thank you, and apologies on behalf of the Ranch.
 
    Bookmark Topic Watch Topic
  • New Topic