The Java for-each loop provides a straightforward and convenient method to iterate over elements within an array or collection individually.
Java for-each Loop
The enhanced for loop, also known as the Java for-each loop, was introduced in J2SE 5.0. This loop offers a different way to iterate over arrays or collections in Java and is commonly employed for traversing elements within arrays or collections.
Advantages of Java for-each loop
One key benefit of using the for-each loop is its ability to prevent common programming errors like index-out-of-bound issues. This loop enhances code cleanliness, conciseness, and readability. It is named as a for-each loop because it automatically processes each element individually.
Limitations of for-each Loop
One limitation of the enhanced for loop is its inability to iterate through elements in a reverse order. With this loop, there is no capability to selectively skip elements since it operates without indexing. Additionally, it does not support traversing exclusively odd or even elements.
When to Use for-each Loop
Using the Java for-each loop is advised when iterating through arrays and collections as it enhances code readability.
Syntax of for-each Loop
In Java, the for-each loop syntax involves specifying the data type along with a variable, then using a colon (:) before the array or collection.
for(data_type variable : array | collection){
//body of for-each loop
}
How for-each Loop Works?
The Java for-each loop iterates through the array or collection until reaching the final element. During each iteration, it assigns the element to a variable and runs the code block within the for-each loop.
The following are the steps:
- The loop starts with the first element of the array or collection.
- The current element is stored in the loop variable.
- The statements inside the loop body are executed.
- The process repeats for each element until the last element is reached.
for each Loop Examples
Work through these instances to grasp the idea of the for each loop in Java.
Example 1: Traversing Array Elements
In this instance, the for-each loop in Java sequentially accesses each element within the arr array. Within each iteration, the value of the array at that point is assigned to the variable i, which is then output using System.out.println(i). The loop iterates through all elements of the array until each one has been printed.
Example
//An example of Java for-each loop
class Main{
public static void main(String args[]){
//declaring an array
int arr[]={12,13,14,44};
//traversing the array with for-each loop
for(int i:arr){
System.out.println(i);
}
}
}
Output:
12
13
14
44
Example 2: Calculating Sum of Array Elements
In this instance, the Java for-each loop sequentially processes each item within the arr array. In each cycle, the ongoing element gets assigned to the variable i and then summed up in the total variable. Upon completion of the loop, the software displays the cumulative sum of all array elements.
Example
class Main{
public static void main(String args[]){
int arr[]={12,13,14,44};
int total=0;
for(int i:arr){
total=total+i;
}
System.out.println("Total: "+total);
}
}
Output:
Total: 83
Example 3: Traversing Collection Elements
This illustration showcases the utilization of the Java for-each loop to iterate through an ArrayList and display each element individually.
Example
import java.util.*;
class Main{
public static void main(String args[]){
//Creating a list of elements
ArrayList<String> list=new ArrayList<String>();
list.add("vimal");
list.add("sonoo");
list.add("ratan");
//traversing the list of elements using for-each loop
for(String s:list){
System.out.println(s);
}
}
}
Output:
vimal
sonoo
ratan