File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

iterator method of ArrayList class

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 4
  • Mark post as helpful
  • send pies
  • 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 ?
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 4
  • Mark post as helpful
  • send pies
  • 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 ?
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 2
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 1
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • 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 ?
.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 1
  • Mark post as helpful
  • send pies
  • 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....
 
Winston Gutkowski
Bartender
Pie
Posts: 9488
50
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 5
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic