Java for each loop | Enhanced For Loop Java Example

Java for-each loop is used to traverse array or collection elements(items). It introduced in Java version 5. In this tutorial, you will learn how to use a for-each loop with Array and List to get access elements of it.

It’s also called “Java Advanced for loop” or “Enhanced for loop java“. It can be an alternative to for loop in Java, sometimes you find it’s a short version of for-loop.

Java for each loop Enhanced For Loop Java Example array and list

Before starting:

Must read this tutorial, basic about the Enhanced for loop in java.

Syntax

Here is Java for each syntax and details.

for(data_type item : collection) {
    ...
}
  • Item/Element: A single entity(item/elements) from a collection.
  • Collection: A data formate to use go through in a loop.

How for each loop works?

  • It starts with the first time and store value in the variable, then goes through the body.
  • Check there is the next item or not, if not then closed the loop else again to the body.

With an example, you will get more clarity.

for each Flowchart Diagram

java for each Flowchart Diagram

First Java for each example

Example of for loop program to the sum of all elements of an integer array.

class Hello {
    public static void main(String[] args) {

        int[] numbers = {1, 2, 3, 4, 5};
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }
        System.out.println("Sum = " + sum);
    }
}

Output: Sum = 15

Java for each Array Example

Traversing the array elements using a for-each loop, see the below  Simple Example program.

class ForEachExample {
    public static void main(String args[]) {
        int arr[] = {21, 31, 41};

        for (int i : arr) {
            System.out.println(i);
        }
    }
}

Output: 21
31
41

 

for each list Example in Java

Now traversing the collection(List) elements with for each loop here is an example of Java for each list.

import java.util.ArrayList;
import java.util.List;

class ForEachExample {
    public static void main(String args[]) {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(5);
        list.add(8);

        for (Integer s : list) {
            System.out.println(s);
        }
    }
}

Output: 2
5
8

QA: How to use Java for-each loops to display elements of an array?

Answer: Simply use the below code.

for (int number : numbers) {
            System.out.println(number);
        }

See the above section example of “for each Array”.

QA: What is the Advantage of the for-each loop in Java?

  • The code is more readable with a for-each loop
  • It eliminates the possibility of programming errors and bugs.

Do comment if any doubt, suggestions or code, you think should be in the tutorial for other help.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Examples of foreach loop are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.