Enhanced for loop:

for (String element : array) {

    // rest of code handling current element
}

Traditional for loop equivalent:

for (int i=0; i < array.length; i++) {
    String element = array[i]; 

    // rest of code handling current element
}

Take a look at these forums: https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with

http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html

Answer from user1920811 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-for-loop-and-enhanced-for-loop-in-java
Java for loop vs Enhanced for loop - GeeksforGeeks
July 23, 2025 - ... for (data_Type_element : ... java.io.*; import java.util.*; class Geeks { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Iterate through the array // using an enhanced for ...
🌐
Zero To Mastery
zerotomastery.io › blog › enhanced-for-loop-java
How To Use Enhanced For Loops In Java (aka 'foreach') | Zero To Mastery
January 26, 2024 - In this example, each Expense object in the List is processed alongside a corresponding adjustment value, stored in a parallel array. This operation requires simultaneous access to both the expenses list and the adjustments array, which can ...
🌐
CodeGym
codegym.cc › java blog › java collections › enhanced for loop in java
Enhanced for loop in Java
February 13, 2025 - The enhanced for loop is not only easier to read and write, but it's also safer. It eliminates the possibility of off-by-one errors and makes the code more concise and readable. When working with Map collections, the for-each loop is useful for iterating over key-value pairs using entrySet(). import java.util.HashMap; import java.util.Map; public class MapIteration { public static void main(String[] args) { Map
🌐
W3Schools
w3schools.com › java › java_for_loop.asp
Java For Loop
In this example, the loop starts with i = 10. The condition i < 5 is already false, so the loop body is skipped, and nothing is printed. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an ...
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › how-does-enhanced-for-loop-works-in-java.html
How foreach or Enhanced for loop works in Java? Example
July 21, 2023 - Both while and for in combination with ListIterator and Iterator give more flexibility and power as you can iterate both forward and backward, but yes for other Collection e.g. a Set or a Queue which doesn't support ListIterator, enhanced for loop is same as using Iterator with for or while loop. here is a nice summary of why enhanced for loop was introduced in Java 5 and why you should use it, along with syntax and examples of how to use it for iterating over collection and array.
Find elsewhere
🌐
Java67
java67.com › 2019 › 06 › 5-examples-of-enhanced-for-loop-in-java.html
5 Examples of Enhanced for loop in Java | Java67
Learn Java and Programming through articles, code examples, and tutorials for developers of all levels. ... Enhanced for loop was added way back in 2004 in Java and it provides an easy and cleaner way to iterate over array and Collection in Java. The introduction of forEach() in Java 8 has further improved iteration but that doesn't mean that you need to forget the for each loop.
🌐
freeCodeCamp
freecodecamp.org › news › enhanced-for-loops-in-java-how-to-use-foreach-loops-on-arrays
Enhanced For Loops in Java – How to Use ForEach Loops on Arrays
February 17, 2023 - Here's what the syntax of a for-each loop in Java looks like: for(dataType variable : array) { // code to be executed } ... variable denotes a variable assigned to each element in the array during the iteration (you'll understand this through ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit7-Arrays › topic-7-3-arrays-with-foreach.html
7.3. Enhanced For-Loop (For-Each) for Arrays — CS Java
To set up a for-each loop, use for (type variable : arrayname) where the type is the type for elements in the array, and read it as “for each variable value in arrayname”. for (type item: array) { //statements using item; } See the examples below in Java that loop through an int and a String ...
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › for.html
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
To demonstrate, consider the following array, which holds the numbers 1 through 10: ... class EnhancedForDemo { public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } } } In this example, the variable ...
🌐
Programiz
programiz.com › java-programming › enhanced-for-loop
Java for-each Loop (With Examples)
In this tutorial, we will learn about the Java for each loop and its difference with for loop with the help of examples. The for-each loop is used to iterate each element of arrays or collections.
🌐
TutorialsPoint
tutorialspoint.com › what-is-enhanced-for-loop-in-Java
what is enhanced for loop in Java?
June 15, 2020 - In this example, we're showing the use of a foreach loop to print contents of an List of String. Here we're creating an array of Strings as names and initialized it some values. Then using foreach loop, each name is printed. import java.util.Arrays; ...
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › how-does-enhanced-for-loop-works-in-java.html
Javarevisited: How foreach or Enhanced for loop works in Java? Example
Both while and for in combination with ListIterator and Iterator give more flexibility and power as you can iterate both forward and backward, but yes for other Collection e.g. a Set or a Queue which doesn't support ListIterator, enhanced for loop is same as using Iterator with for or while loop. here is a nice summary of why enhanced for loop was introduced in Java 5 and why you should use it, along with syntax and examples of how to use it for iterating over collection and array.
🌐
Javatpoint
javatpoint.com › for-each-loop
Java For-each Loop | Enhanced For Loop - javatpoint
Java For-each loop | Java Enhanced For Loop: The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.
🌐
Tutorjoes
tutorjoes.in › java_programming_tutorial › Enhanced_for_in_java
Enhanced for loop in Java
Overall, this program demonstrates how to use an enhanced for loop to iterate through an array and perform a task on each element. public class Enhanced_for { public static void main(String args[]) { int numbers[]={10,20,30,40,50,60,70}; for(int n : numbers) { System.out.println(n); } } }
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit6-Arrays › topic-6-3-arrays-with-foreach.html
6.3. Enhanced For-Loop (For-Each) for Arrays — CSAwesome v1
In this challenge, we will create ... in the array using enhanced for loops. We encourage you to continue working in pairs. Here is an example of a Student class and a StudentArray class that searches for a student with a specific name. In Java, when you are working with ...
🌐
Minich
minich.com › education › wyo › java › lecture_notes › enhanced_for_loops.php
Enhanced For Loops
A one-dimensional array can be used as the loop variable in an enhanced for loop. For example, here's a method that finds the sum of all the elements of a two-dimensional array of integers.
🌐
JetBrains
jetbrains.com › help › inspectopedia › ForLoopReplaceableByForEach.html
'for' loop can be replaced with enhanced for loop | Inspectopedia Documentation
December 3, 2025 - Use the Report indexed 'java.util.List' loops option to find loops involving list.get(index) calls. Generally, these loops can be replaced with enhanced for loops, unless they modify an underlying list in the process, for example, by calling list.remove(index).
🌐
FavTutor
favtutor.com › blogs › java-enhanced-for-loop
Enhanced For Loop Java (with Codes)
December 1, 2023 - Java's improved for loop makes handling collections and arrays easier for programmers. This simple construct, in contrast to conventional loops, simplifies iteration through elements, providing a cleaner syntax and lowering errors. The mechanics of the enhanced for loop, how it differs from ...