• 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:
  • 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 vs for loop

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the advantage of Iterator over for loop?

Thanks
 
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that's a great question.

I think 99% of the iterators that I ever used simply iterate over the entire collection. With the "for each" loop, you never need to program those iterators again. (The compiler generates the iterator calls for you.)

Once in a rare while, you'll have a more complex iteration protocol, maybe with two iterators, one starting at the front, the other at the back. Then the "for each" loop won't work, and you have to program with explicit iterators.

Cheers,

Cay
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cay, I did not understand what you meant by:

With the "for each" loop, you never need to program those iterators again. (The compiler generates the iterator calls for you.)


Could you please explain it?
My understanding of Iterator is as mentioned below, correct me if I am wrong.


Using an Iterator to iterate through a Collection is the safest and fastest way to traverse through a Collection especially when the type of the Collection is not known.

For example, say you have a LinkedList. If you traverse the LinkedList using a for loop it will be slower than if you had used the Iterator. This is because the iterator for the LinkedList knows best how to traverse its collection.

For collections like ArrayList and Vector, which are array-backed, it might not make a difference which method you choose.

Iterators provide added facility to "remove" an object during iteration. Try that in a for loop and you are bound to face problems.

[Edit: I guess my explanation only applies to "List" type collections, so please read "Collection" as "List". From the subject line "Iterator vs for loop", I assumed that List was implied, since you do find a lot of people using traditional for loops to iterate through Lists.]
Sheldon Fernandes
[ November 16, 2004: Message edited by: Sheldon Fernandes ]
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cay, I was just looking at some of your replies and realized that "for each" is a feature of the new Java release. Pardon my ignorance, haven't had a chance to explore the new release yet.
 
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would for each loop having better performance as compare traditional for loop ? or it just a shortcut to simplify for loop ?
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion ::

For each loop is use when you want to display(read only) data in object like array, collection .. etc. Because in For each loop is read only. (Different with traditional for loop) :: Readable.

Case iterator, it good for access collection framework object, you can access object in order and can be changed object in collection object. (Good and easy)
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sheldon, I think the confusion about your question comes from the fact that we usually use an Iterator in a for loop:It seems you're asking us to compare the above withThe description of Iterator that you posted is quite correct. The Iterator will be faster since a LinkedListIterator has knowledge of the underlying data structure and traverses the list directly. When you call get(i) on a LinkedList, it starts at the head of the list and follows the "next" pointers until it reaches the ith element.

Let's say you have a LinkedList with 100 elements. Calling get(50) results in 50 Node traversals -- node.getNext(). The next call, get(51) results in 51 traversals. Contrast that with an Iterator pointing at element 50. A call to next() on the Iterator results in a single node traversal from the current node to the next node.

Thus, using an Iterator over a LinkedList with n elements requires n traversals while using a for loop and get(i) requires 1 + 2 + 3 + ... + n = n * (n + 1) / 2 traversals.

Some Collection classes aren't affected in this way, and some are worse. That's the beauty of an Iterator. It allows the author to best implement the Iterator for that class and you don't have to worry about it.
[ November 17, 2004: Message edited by: David Harkness ]
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iterator protected ArrayIndexOutOfBoundException and can't specified index of object of Collection object.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Iterator protected ArrayIndexOutOfBoundException and can't specified index of object of Collection object.


In fact, I guess this is a point of view of different methods. Although we dont need to check for the size on iterator, we still need to know whether it is null (or has next). Thus, the argument is just for checking *explicitly* whether the size = 0, or the iterator has next element.

Nick
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic