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 method of ArrayList class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

iterator method of ArrayList class

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Hi folk
My question is that if i can iterate an ArrayList using for loop then what does iterator() make difference here ?


Tell the difficulties that i am difficult.
Panagiotis Kalogeropoulos
Rancher

Joined: May 27, 2011
Posts: 99

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

Joined: May 27, 2011
Posts: 99

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

Joined: Feb 09, 2012
Posts: 583

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 ?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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

Joined: May 27, 2011
Posts: 99

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 ?
.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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....
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: iterator method of ArrayList class
 
Similar Threads
iterating through vector - ListIterator
Blocking I/O using Iterator()
What is a Iterator class?
Vector and hashtable inTiger
Iterator of an ArrayList