Java: For each loop

The for each loop gives an easy way to iterate through arrays.

On each iteration the next element in the array is assigned to the specified variable, and the loop continues to execute until it has gone through the entire array.

package javaapplication9;

public class JavaApplication9 {

    public static void main(String[] args) {

        int[] array = {1, 2, 3, 4, 5};
        
        for (int element : array) {
            System.out.print(element);
        }
        System.out.println();
    }
}

foreach

Series Navigation<< Java: Array
Java: Array size >>