aspose file tools*
The moose likes Java in General and the fly likes two ways of looping into a list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "two ways of looping into a list" Watch "two ways of looping into a list" New topic
Author

two ways of looping into a list

Kaylee Sebi
Greenhorn

Joined: Jun 29, 2007
Posts: 10
When I want to loop through an ArrayList aList, instead doing the following:

Itereator it= aList.iterator();
while ( it.hasNext() ){...}

Another way is:

for(Iterator ii = aList.iterator(); ii.hasNext(); ){...}

What is happening under the scene in these two scenarios ?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The best way to find out is to use javap to take a look at the byte code for both loops.

My guess would be that they are (near to) identical.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39877
    
  28
I was taught that the "correct" way to iterate an ArrayList is to use an Iterator, rather than a "for" loop with a counter. But recently I came across the RandomAccess tagging interface; it suggests that a for loop with a counter may be faster for an ArrayList. Have a look at the API docs for ArrayList and RandomAccess.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14351
    
  22

Neither of the examples that Kaylee shows involves a for-loop with a counter.

If you are using Java 5 or newer and you don't need explicit access to the iterator, then the shortest and clearest way to iterate over a list is to use the new "for each" syntax:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In a Java 1.3 and 1.4 environment at work, I've recently started using Keylee's 2nd example style with a for loop and an iterator. I resisted for a while because I thought for loops so often used an int counter or index that it might confuse readers to do something else. I guess getting rid of a line of code and a variable scoped outside the loop finally won me over.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jesper Young:

If you are using Java 5 or newer and you don't need explicit access to the iterator, then the shortest and clearest way to iterate over a list is to use the new "for each" syntax


True - it will be the shortest and clearest source code, which is important. I'd again guess that the compiled byte code will be still near to identical.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: two ways of looping into a list