tags:

views:

12088

answers:

11
List<String> someList = new ArrayList<String>()
// add "monkey", "donkey", "skeleton key" to someList
for(String item : someList ){
   System.out.println(item);
}

What would the equivalent for loop look like without using the for each syntax?

+17  A: 
for(Iterator<String> i = someList.iterator(); i.hasNext(); ) {
  String item = i.next();
  System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for( : ) idiom, since the actual Iterator is merely inferred.

As was noted by Denis Bueno, this code works for any object that implements the Iterable interface.

Also, if the right-hand side of the for(:) idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array.length instead. See http://forums.sun.com/thread.jspa?messageID=2743233

nsayer
+3  A: 

The for-each loop in java uses the underlying iterator mechanism. So it's identical to the following:

Iterator<String> iterator = someList.iterator();

while (iterator.hasNext()) {
  String item = iterator.next();
  System.out.println(item);
}
toluju
A: 

Check here

jb
A: 

Here's an equivalent expression.

for(Iterator<String> sit = someList.iterator(); sit.hasNext(); ) {
    System.out.println(sit.next());
}
Hank
A: 

It would look something like this. Very crufty.

for (Iterator<String> i = someList.iterator(); i.hasNext(); )
        System.out.println(i.next());

There is a good writeup on for each in the Sun documentation.

Pete
+1  A: 

for (Iterator<String> itr = someList.iterator(); itr.hasNext(); ) {
String item = itr.next();
System.out.println(item);
}

+3  A: 

It's implied by nsayer's answer, but it's worth noting that the OPs for(..) syntax will work when "someList" is anything that implements java.lang.Iterable -- it doesn't have to be a list, or some collection from java.util. Even your own types, therefore, can be used with this syntax.

Denis Bueno
Also, by some cunning magic object arrays also work.
fd
fd is right - the internal code when the right-hand side of the for(:) idiom uses an int and array.length instead of fetching an Iterator. http://forums.sun.com/thread.jspa?messageID=2743233
nsayer
A: 

Also note that using the "foreach" method in the original question does have some limitations, such as not being able to remove items from the list during the iteration.

The new for-loop is easier to read and removes the need for a separate iterator, but is only really usable in read-only iteration passes.

Bill James
A: 

@rtf

It isn't by index, as not everything that implements Iterable is a list. It could be a set or map or anything else that implements iterable.

John Gardner
+3  A: 
Mikezx6r
Hmm, what was the point in repeating all the information?
Konrad Rudolph
+1  A: 

The Java "for-each" loop construct will allow iteration over two types of objects:

  • T[] (arrays of any type)
  • java.lang.Iterable<T>

The Iterable<T> interface has only one method: Iterator<T> iterator(). This works on objects of type Collection<T> because the Collection<T> interface extends Iterable<T>.

Ryan Delucchi