Sunday, November 16, 2008

cooler for-loops

JDK 5 offers an interesting enhancement for for-loops. With the new feature, for-loops are less error-prone and with a higher readability. You can convert your for-loop from
for(int i=0; i < integerArray.length; i++) {
System.out.println( integerArray[i] );
}

to
// read below as int i "in" integerArray
for(int i : integerArray){
System.out.println( i );
}

Better, huh? Same stuff works with object arrays too.

No comments:

Post a Comment