views:

466

answers:

5

I am fairly new to Java and in another Stack Overflow question about for loops an answer said that there was two uses of for in Java:

for (int i = 0; i < N; i++) {
}


for (String a : anyIterable) {
}

I know the first use of for and have used it a lot, but I have never seen the second one. What is it used to do and when would I use it?

+11  A: 

The first of the two you specify is a classic C for loop. This gives the programmer control over the iteration criteria and allows for three operations: the initialization; the loop test ; the increment expression. Though it is used often to incrementally repeat for a set number of attempts, as in yor example:

for (int i=0; i < N : i++)

There are many more instances in code where the for was use to iterate over collections:

for (Iterator iter = myList.iterator(); iter.hasNext();)

To aleviate the boilerplating of the second type (where the third clause was often unused), and to compliment the Generics introduced in Java 1.5, the second of your two examples - the enhanced for loop, or the for-each loop - was introduced.

The second is used with arrays and Generic collections. See this documentation. It allows you to iterate over a generic collection, where you know the type of the Collection, without having to cast the result of the Iterator.next() to a known type.

Compare:

for(Iterator iter = myList.iterator; iter.hasNext() ; ) {
   String myStr = (String) iter.next();
   //...do something with myStr
}

with

for (String myStr : myList) {
   //...do something with myStr
 }

This 'new style' for loop can be used with arrays as well:

String[] strArray= ...
for (String myStr : strArray) {
   //...do something with myStr
}
akf
Or array. Or regular collection if your variable is of type Object :-)
ChssPly76
It is not exclusive to generic collections.
Pavel Minaev
@pavel - i am willing to make this a wiki answer if you want to add more to it.
akf
yeah this has nothing to do with generics. it is just the foreach loop that works on all types that implement Iterable (which includes all Collections). the reason that you need to do that cast is because you are not using the generic parameter of Iterator. it should be: "for (Iterator<String> iter = myList.iterator(); iter.hasNext(); ) ... String myStr = iter.next(); ..."
newacct
+1  A: 

It's used to iterate through an entire array or Iterable collection. It basically replaces:

for(int i=0; i<N.length; i++)

And can be used with Generics which is quite cool

Paul
+1  A: 

Here is a link - Enhanced for-loop

adatapost
+4  A: 

The "for in" loop is mostly eye-candy that makes code easier to read. (If you pronounce the : as "in").

I use the second type almost exclusively in my own code whenever I'm iterating over a list or array.

I still use the regular for loop when dealing with mostly mathematical code, or when I need an "i" to see how many times I've looped.

wm_eddie
A: 

It's called a for each loop and basically, it can be used with anything that implements

java.lang.Iterable

Generic collections implement this interface, but you could make your own class that did as well. It makes dealing with everything in a list/collection very easy although the downside is that you don't get explicit access to the current number in the list like you do in the traditional loop. There is some additional overhead associated with this type of loop over traditional loops because of the calls to the iterator classes.

Chris Thompson