How to use for each loop through an array in Java?


JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.

Example

Live Demo

public class ArrayUsingForEach {
   public static void main(String[] args) {
      double[] myList = {1.9, 2.9, 3.4, 3.5};
      for (double element: myList) {
         System.out.println(element);
      }
   }
}

Output

1.9
2.9
3.4
3.5

Updated on: 12-Mar-2020

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements