A “for” loop is essential in the programming languages to iterate along the contained or specified elements in a stepwise process. However, the enhanced “for” loop i.e., “for-Each” performs the iteration smartly. It is such that it does not require any additional functionality such as the “length” property to perform the iteration. Rather it traverses to each of the contained elements.
What is Java Enhanced for Loop?
The enhanced “for” loop, also referred to as the “for-each” loop, is introduced in Java 5 that is considered to be a concise approach to iterate along arrays, collections, and other objects that implements the “Iterable” interface.
Syntax
for(dt variable : container) {
// code to be executed
}
In this syntax:
- “dt” refers to the container’s data type.
- “variable” denotes a variable allocated to each array element in the container.
- “container” corresponds to “array”, “list” etc. that needs to be iterated.
Example 1: Applying the Enhanced for Loop (for-Each) to Iterate Along an Array
This example applies the enhanced “for” loop to iterate through the integer array:
public class Enhancedloop {
public static void main(String[] args) {
int[] arrayInt = {1, 2, 3};
for (int values : arrayInt) {
System.out.println(values);
}
}}
In the above code lines, define an array of integers named “arrayInt”. After that, apply the enhanced “for” loop to iterate the integer values in an array and return them.
Output

In this output, it can be observed that the integers in an array are iterated appropriately.
Example 2: Applying the Enhanced for Loop (for-Each) to Perform an Arithmetic Operation i.e., Addition Upon all the Array Elements
This demonstration implements the enhanced “for” loop to add all the array elements i.e., integers:
public class Enhancedloop {
public static void main(String[] args) {
int[] values = {1, 2, 3};
for(int item : values){
item = item + 3;
System.out.println(item);
}
}}
In this code, apply the enhanced “for” loop upon the integer array and apply the arithmetic operation “+” such that “3” is added to each array element.
Output

As analyzed, all the array elements are added/modified accordingly.
Example 3: Applying the Enhanced for Loop (for-Each) to Print a 2D Array
In this specific example, the enhanced “for” loop can be used to return all the elements of a 2-dimensional array:
public class Enhancedloop {
public static void main(String[] args) {
int[][] a= {{1, 2}, {3, 4}} ;
for(int[] items :a){
for(int secitems : items) {
System.out.println(secitems);
}}
}}
According to these lines of code:
- Create a 2D array comprising the stated integers.
- Now, apply the enhanced “for” loop twice to access each 2D array element.
- Finally, return the iterated elements.
Output

Include the below-given package in the next example to access all the features in the “java.util” package:
import java.util.*;
Example 4: Applying the Enhanced for Loop (for-Each) to Filter a List
In this demonstration, the discussed loop can be utilized to filter a list:
public class Enhancedloop {
public static void main(String args[]) {
List<String> values = Arrays.asList("Linuxhint", "Java", "Linux");
List<String> filterList = new ArrayList<>();
for (String item : values)
if (item.startsWith("L")) {
filterList.add(item);
}
System.out.println("Filtered Elements -> " + filterList);
}}
Based on this snippet of code:
- Create a list of “String” types comprising the provided values.
- In the next step, define a list that needs to be appended with the filtered values.
- Now, apply the enhanced “for” loop that accumulates the “startsWith()” and “add()” methods to filter the list such that the updated list gets appended with the values starting with the ‘L’ character in the former list.
Output

As verified, the filtered values are appended in the list accordingly.
Conclusion
The enhanced for loop, also known as the “for-each” loop, is a functionality that iterates through each of the values without any additional functionality i.e., function, or method. This blog discussed the working of the enhanced “for” loop in Java.