Java forEach Array Example

In this source code example, we show how to use forEach() method to loop over an Array in Java with examples.

Java forEach Array Example

In below example, we loop int and string array using forEach() method:


import java.util.Arrays;

public class ForEachArray {

    public static void main(String[] args) {

        int[] nums = { 3, 4, 2, 1, 6, 7 };

        Arrays.stream(nums).forEach(System.out::println);
        
        String[] numsStr = { "Three", "Four", "Two", "One"};

        Arrays.stream(numsStr).forEach(System.out::println);
    }
}

Output:

3
4
2
1
6
7
Three
Four
Two
One

Comments