Enhanced for Statement

In previous examples, we demonstrated how to use counter-controlled for statements to iterate through the elements in an array. In this section, we introduce a feature new in J2SE 5.0the enhanced for statement, which iterates through the elements of an array or a collection without using a counter. This section discusses how to use the enhanced for statement to loop through an array. We show how to use the enhanced for statement with collections in Chapter 19, Collections. The syntax of an enhanced for statement is:

       for ( parameter : arrayName )

          statement

where parameter has two partsa type and an identifier (e.g., int number), and arrayName is the array through which to iterate. The type of the parameter must match the type of the elements in the array. As the next example illustrates, the identifier represents successive values in the array on successive iterations of the enhanced for statement.

Figure 7.12 uses the enhanced for statement to calculate the sum of the integers in an array of student grades (lines 1213). The type specified in the parameter to the enhanced for is int, because array is an array containing int valuestherefore, the loop will select one int value from the array during each iteration. The enhanced for statement iterates through successive values in the array one-by-one. The enhanced for header can be read concisely as "for each iteration, assign the next element of array to int variable number, then execute the following statement." Thus, for each iteration, identifier number represents an int value in array. Lines 1213 are equivalent to the following counter-controlled repetition used in lines 1213 of Fig. 7.5 to total the integers in array:

 

for ( int counter = 0; counter < array.length; counter++ ) total += array[ counter ];

 

Figure 7.12. Using the enhanced for statement to total integers in an array.

1 // Fig. 7.12: EnhancedForTest.java 2 // Using enhanced for statement to total integers in an array. 3 4 public class EnhancedForTest 5 { 6 public static void main( String args[] ) 7 { 8 int array[] = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 }; 9 int total = 0; 10 11 // add each element's value to total 12 for ( int number : array ) 13 total += number; 14 15 System.out.printf( "Total of array elements: %d ", total ); 16 } // end main 17 } // end class EnhancedForTest  

Total of array elements: 849  

The enhanced for statement simplifies the code for iterating through an array. Note, however, that the enhanced for statement can be used only to access array elementsit cannot be used to modify elements. If your program needs to modify elements, use the traditional counter-controlled for statement.

The enhanced for statement can be used in place of the counter-controlled for statement whenever code looping through an array does not require access to the counter indicating the index of the current array element. For example, totalling the integers in an array requires access only to the element valuesthe index of each element is irrelevant. However, if a program must use a counter for some reason other than simply to loop through an array (e.g., to print an index number next to each array element value, as in the examples earlier in this chapter), use the counter-controlled for statement.

Категории