Program Flow  «Prev  Next»
Lesson 5Iterate over Collection
ObjectiveHow to use for-each loop in Java

for-each statement

Java for-each Loop: Iterate over collection

The for-each statement was introduced with the release of Java 5. It is sometimes referred to as the enhanced for loop. Advantages of using the for-each statement include:
  1. It is unnecessary to provide end conditions for the counter variable
  2. It is simpler and more readable
  3. The statement provides opportunities for compiler optimization
  4. The use of generics is simplified

The Java for loop

The for-each statement is used in conjunction with collections and arrays. It provides an easier way to iterate through each member of an array or class that has implemented the java.util.Iterable interface. As the Iterable interface is the super interface of the java.util.Collection interface, the for-each statement can be used with those classes that implement the Collection interface. The syntax of this statement is similar to the regular for statement, except for the contents of its parentheses. The contents include a data type followed by a variable, a colon, and then an array name or collection, illustrated as follows:
for (<dataType variable>:<collection/array>)
//statements;

In the following sequence, an array of integers is declared, initialized, and a for-each statement is used to display each element of the array:

int numbers[] = new int[10];
for (int i = 0; i < 10; i++) {
 numbers[i] = i;
}
for (int element : numbers) {
 System.out.print(element + " ");
}
System.out.println();
The elements of the numbers array were initialized to their index. Notice that a for statement was used. This was because we are unable to access an index variable directly in a for-each statement easily. The for-each statement in the preceding code snippet is read as "for each element in numbers". During each iteration of the loop, element corresponds to an element of the array. It starts with the first element and ends with the last element. The output of this sequence is as follows:

0 1 2 3 4 5 6 7 8 9

Disadvantages of for-each statement in Java

There are drawbacks to the use of the for-each statement with an array. It is not possible to do the following:
  1. Modify the current position in an array or list
  2. Directly iterate over multiple arrays or collections

For example, using the previous example, if we try to modify the element of the array containing a 5 with the following code, it will not result in a syntax error. But it also will not modify the corresponding array element:
for (int element : numbers) {
 if (element == 5) {
  element = -5;
 }
}
for (int element : numbers) {
 System.out.print(element + " ");
}
System.out.println();

The output of this sequence is as follows:
0 1 2 3 4 5 6 7 8 9


If we want to use one loop to access two different arrays, the for-each loop cannot be used. For example, if we want to copy one array to another, we need to use the for loop, shown as follows:
int source[] = new int[5];
int destination[] = new int[5];
for(int number : source){
 number = 100;
}
for(int i = 0; i < 5; i++){
 destination[i] = source[i];
}
While we used a for-each to initialize the source array, we can only address a single array at a time. Thus, in the second loop we were forced to use the for statement.