How do you use enhanced for-each loop with a simple code example?

The enhanced for-each loop in Java simplifies iteration over arrays and collections. It eliminates the need for explicit iteration counters or iterators, making the code cleaner and easier to read.

keywords: Java, enhanced for-each loop, iteration, arrays, collections
description: This example demonstrates the use of the enhanced for-each loop in Java to iterate over an array of integers and print each value.
// Java example using enhanced for-each loop public class EnhancedForEachExample { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; // Using enhanced for-each loop to iterate through the array for (int number : numbers) { System.out.println(number); } } }

keywords: Java enhanced for-each loop iteration arrays collections