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

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course

Comments