Sunday 13 March 2016

The Java Language: The enhanced for loop

Java’s auspiciously dubbed “enhanced for loop” acts like the “foreach” statement in some other languages, iterating over a series of values in an array or other type of collection:

 for ( varDeclaration : iterable )  
 statement;  
The enhanced for loop can be used to loop over arrays of any type as well as any kind of Java object that implements the java.lang.Iterable interface. This includes most of the classes of the Java Collections API. We’ll talk about arrays in this and the next section; We will also cover Java Collections. Here are a couple of examples:
 int [] arrayOfInts = new int [] { 1, 2, 3, 4 };  
 for( int i : arrayOfInts )  
 System.out.println( i );  
 List<String> list = new ArrayList<String>();  
 list.add("foo");  
 list.add("bar");  
 for( String s : list )  
 System.out.println( s );  
Again, we haven’t discussed arrays or the List class and special syntax in this example. What we’re showing here is the enhanced for loop iterating over an array of integers and also a list of string values. In the second case, the List implements the Iterable interface and thus can be a target of the for loop.

0 comments:

Post a Comment