The enhanced for loop, also known as the for-each loop, provides a simplified and concise syntax for iterating over collections and arrays in Java. It is particularly useful when you need to iterate through all elements of a collection or array without explicitly initializing an iterator or dealing with index-based access. Let’s explore how to use the enhanced for loop with examples for iterating over collections like ArrayList, LinkedList, and arrays.
The syntax of the enhanced for loop in Java is as follows:
for (ElementType element : collection) {
   // Code to be executed for each element
}
Here,
ElementType: Specifies the data type of elements in the collection or array.
collection: Represents the collection (e.g., ArrayList, LinkedList) or array over which iteration will occur.
element: The variable that represents each element in the iteration.
1.Iterating Over ArrayList
import java.util.ArrayList; class EnhancedForLoopExample { public static void main(String[] args) { ArrayList<String> students = new ArrayList<>(); students.add("Paani"); students.add("Mahesh"); students.add("Datta"); students.add("Ganesh"); students.add("Harsha"); // Iterating over ArrayList using enhanced for loop System.out.println("Student names in ArrayList:"); for (String student : students) { System.out.println(student); } } } /* D:\javaplanet\Collection Framework>javac EnhancedForLoopExample.java D:\javaplanet\Collection Framework>java EnhancedForLoopExample Student names in ArrayList: Paani Mahesh Datta Ganesh Harsha */
2. Iterating Over LinkedList
Â
import java.util.LinkedList; public class EnhancedForLoopLinkedListExample { public static void main(String[] args) { LinkedList<String> students = new LinkedList<>(); students.add("Paani"); students.add("Mahesh"); students.add("Datta"); students.add("Ganesh"); students.add("Harsha"); // Iterating over LinkedList using enhanced for loop System.out.println("Student names in LinkedList:"); for (String student : students) { System.out.println(student); } } } /* D:\>javac EnhancedForLoopLinkedListExample.java D:\>java EnhancedForLoopLinkedListExample Student names in LinkedList: Paani Mahesh Datta Ganesh Harsha */
- Iterating Over Arrays
class EnhancedForLoopArrayExample { public static void main(String[] args) { String[] students = {"Paani", "Mahesh", "Datta", "Ganesh", "Harsha"}; // Iterating over array using enhanced for loop System.out.println("Student names in array:"); for (String student : students) { System.out.println(student); } } } /* D:\>javac EnhancedForLoopArrayExample.java D:\>java EnhancedForLoopArrayExample Student names in array: Paani Mahesh Datta Ganesh Harsha */
The enhanced for loop (for-each loop) in Java simplifies iteration over collections and arrays, enhancing code readability and reducing complexity. It’s especially useful when you need to perform simple iterations over elements without requiring index-based operations or modifying the collection. Understanding how to effectively use the enhanced for loop enables developers to write cleaner and more maintainable Java code for iterating over data structures.