for (Iterator<String> i = someIterable.iterator(); i.hasNext();) {
    String item = i.next();
    System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for ( : ) idiom, since the actual iterator is merely inferred.

As was noted by Denis Bueno, this code works for any object that implements the Iterable interface.

If the right-hand side of the for (:) idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array.length instead. See the Java Language Specification.

for (int i = 0; i < someArray.length; i++) {
    String item = someArray[i];
    System.out.println(item);
}
Answer from nsayer on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_foreach_loop.asp
Java For-Each Loop
The for-each loop is simpler and more readable than a regular for loop, since you don't need a counter (like i < array.length). The following example prints all elements in the cars array:
🌐
Programiz
programiz.com › java-programming › enhanced-for-loop
Java for-each Loop (With Examples)
Here, we have used the for-each loop to print each element of the numbers array one by one.
🌐
GeeksforGeeks
geeksforgeeks.org › java › for-each-loop-in-java
For-Each Loop in Java - GeeksforGeeks
3 weeks ago - Explanation: In the above example, we use the for-each loop to iterate the list of integer to find the largest or maximum value in the list.
Top answer
1 of 16
1310
for (Iterator<String> i = someIterable.iterator(); i.hasNext();) {
    String item = i.next();
    System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for ( : ) idiom, since the actual iterator is merely inferred.

As was noted by Denis Bueno, this code works for any object that implements the Iterable interface.

If the right-hand side of the for (:) idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array.length instead. See the Java Language Specification.

for (int i = 0; i < someArray.length; i++) {
    String item = someArray[i];
    System.out.println(item);
}
2 of 16
559

The construct for each is also valid for arrays. e.g.

String[] fruits = new String[] { "Orange", "Apple", "Pear", "Strawberry" };

for (String fruit : fruits) {
    // fruit is an element of the `fruits` array.
}

which is essentially equivalent of

for (int i = 0; i < fruits.length; i++) {
    String fruit = fruits[i];
    // fruit is an element of the `fruits` array.
}

So, overall summary:
[nsayer] The following is the longer form of what is happening:

for(Iterator<String> i = someList.iterator(); i.hasNext(); ) {
  String item = i.next();
  System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for( : ) idiom, since the actual Iterator is merely inferred.

[Denis Bueno]

It's implied by nsayer's answer, but it's worth noting that the OP's for(..) syntax will work when "someList" is anything that implements java.lang.Iterable -- it doesn't have to be a list, or some collection from java.util. Even your own types, therefore, can be used with this syntax.

🌐
Tutorialspoint
tutorialspoint.com › java › java_foreach_loop.htm
Java - for each Loop
Then using foreach loop, each name ... Test { public static void main(String args[]) { List<String> names = Arrays.asList("James", "Larry", "Tom", "Lacy"); for( String name : names ) { System.out.print( name ); System.out.print(","); } } } ... In this ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › forEach
Array.prototype.forEach() - JavaScript - MDN Web Docs
The following example illustrates an alternative approach, using forEach(). The following code logs a line for each element in an array:
🌐
Scaler
scaler.com › home › topics › java › for-each loop in java
For-each Loop in Java - Scaler Topics
March 22, 2024 - Note: The for-each loop runs for the whole iterable. ... In the example given above, we have used a for-each loop to print all the elements of the array arr one by one without even passing the index of the array elements.
🌐
Baeldung
baeldung.com › home › java › core java › the for-each loop in java
The for-each Loop in Java | Baeldung
January 8, 2024 - In this tutorial, we’ll discuss the for-each loop in Java along with its syntax, working, and code examples.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › guides › language › foreach.html
The For-Each Loop
1 week ago - In order to fix it, you have to add a variable in the scope of the outer loop to hold the suit: // Fixed, though a bit ugly for (Iterator i = suits.iterator(); i.hasNext(); ) { Suit suit = (Suit) i.next(); for (Iterator j = ranks.iterator(); j.hasNext(); ) sortedDeck.add(new Card(suit, j.next())); } So what does all this have to do with the for-each construct?
🌐
Runestone Academy
runestone.academy › runestone › static › JavaReview › ArrayBasics › aForEach.html
8.2. Looping with the For-Each Loop — AP CSA Java Review - Obsolete
The for-each loop is shown on line 6 above. It says to loop through the array called values and each time through the loop set the variable val to the next item in the array. We have to specify the type of val first since this declares a variable.
🌐
DigitalOcean
digitalocean.com › community › tutorials › foreach-loop-c-plus-plus
C++ foreach Loop: Modern Patterns and AI-Optimized Code | DigitalOcean
August 18, 2025 - Inside the loop structure, ‘i’ is the variable that stores the value of the current array element · arr is the array name which also serves as the base address of the respective array · As we can see, printing ‘i’ for each iteration gives us the corresponding array elements in contrast to the array indices in case of normal for loop
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › statements › iteration-statements
Iteration statements -for, foreach, do, and while - C# reference | Microsoft Learn
January 20, 2026 - The foreach statement enumerates the elements of a collection and executes its body for each element of the collection. The do statement conditionally executes its body one or more times. The while statement conditionally executes its body zero or more times. At any point within the body of an iteration statement, you can exit the loop by using the break statement.
🌐
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.
🌐
W3Schools
w3schools.com › php › php_looping_foreach.asp
PHP foreach loop
Explanation: For each loop iteration, the value of the current array element is assigned to the variable $value.
🌐
W3Schools
w3schools.com › cpp › cpp_for_loop_foreach.asp
C++ The foreach Loop (Ranged for-loop)
You can also use a for-each loop to loop through characters in a string: string word = "Hello"; for (char c : word) { cout << c << "\n"; } Try it Yourself » · Note: Don't worry if you don't understand the examples above.
🌐
Quora
quora.com › What-is-a-for-each-loop-exactly-And-when-do-I-use-it-instead-of-a-for-loop
What is a for each loop exactly? And when do I use it instead of a for loop? - Quora
So in the below example we defined a range of 5 students and after running the loop it will return the names of the students one by one(like Class[0] = ‘Sam’, Class[1] = ‘Rahul’ and so on).
🌐
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 ...
🌐
PHP
php.net › manual › en › control-structures.foreach.php
PHP: foreach - Manual
#include <stdio.h> int main() { int arr[] = {1, 2, 3, 4}; int *current; //First Loop doubles the array for (size_t i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { //arr[i] === *(arr + i) arr[i] *= 2; current = arr + i; printf("Current modified:%d\n", *current); } /* After the loop completes, the arr is now {2,4,6,8}. As for current, it is going to have the memory location of the last element (memory location of 8). */ for (size_t i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { /* In each iteration, the memory location of 8 is going to be dereferenced and set to arr[i]. As a result, the last va
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › for-loop-in-programming
For loop in Programming - GeeksforGeeks
1 week ago - The for-each loop is used to iterate directly over elements of a collection such as arrays or lists without using an index.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_foreach
about_Foreach - PowerShell | Microsoft Learn
January 19, 2026 - In this example, the $letterArray contains the string values a, b, c, and d. The first time the foreach statement runs, it sets the $letter variable equal to the first item in $letterArray (a). Then, it uses Write-Host to display the value.