wood burning stoves 2.0*
The moose likes Java in General and the fly likes Iterator vs for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Iterator vs for loop" Watch "Iterator vs for loop" New topic
Author

Iterator vs for loop

Shanmuga Raja
Greenhorn

Joined: Jun 14, 2004
Posts: 16
What is the advantage of Iterator over for loop?

Thanks


Shanmuga
Cay Horstmann
author
Ranch Hand

Joined: Nov 14, 2004
Posts: 114
    
  10
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


Author of Java 8 for the Really Impatient
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
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

Joined: Aug 18, 2004
Posts: 157
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.
Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
would for each loop having better performance as compare traditional for loop ? or it just a shortcut to simplify for loop ?
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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)


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jul 04, 2003
Posts: 1312
Iterator protected ArrayIndexOutOfBoundException and can't specified index of object of Collection object.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
 
 
subject: Iterator vs for loop
 
Similar Threads
Difference in for loop and Iterator
store list of names in a variable
How many objects are Garbage Collected
Accessing objects from Collection
For each loop