No, but you can provide your own counter.

The reason for this is that the for-each loop internally does not have a counter; it is based on the Iterable interface, i.e. it uses an Iterator to loop through the "collection" - which may not be a collection at all, and may in fact be something not at all based on indexes (such as a linked list).

Answer from Michael Borgwardt on Stack Overflow
🌐
Coderanch
coderanch.com › t › 703218 › java › forEach-loop-starting-index
forEach-loop starting at an index (Beginning Java forum at Coderanch)
That isn't a for‑each loop, but an ordinary for loop. Do you want to iterate the collection repeatedly? Let's have an array which we shall iterate 10×:- ... The � tag will cause ten numbers to be printed across the width of the command line if it has its default width of 80, so you should get twelve lines. ... If you want to start from an index, print the second half of the collection then the first half, try this sort of thing:-
Discussions

[Java] Simple explanation of for each loops
Alright, so a forEach loop is simply some syntactical sugar. Something that makes a common problem or action easier to do. Easier can mean easier in terms of writing it, but also relates to code readability. Anyways, so a normal for loop takes the three conditions that you have stated: for(int i = 0; i < condition; i++ ) { } So I will have a counter here. A super common thing to use a for loop for, is to loop through the contents of an array. So: for(int i = 0; i < myArray.length; i++) { myElement = myArray[i]; } So, at the i-th index, get the element there, and then let's do something with it. But that's a lot of boilerplate ( read: Lot of typing to do something so basic and common ). So, the solution is to use the forEach loop. So we use the syntax: for ( myObject object : myArray ) { } This seems confusing at first, but break it down. All it is saying, in English is: I would like to loop through every element of myArray. During each loop, I would like the current element I am at to be referenced with the variable object which is of type myObject You are now allowed to work with this. But myObject and object are placeholders, and they aren't the easiest to understand. So I will write you a small program that will sum all of the numbers in an array. int integerArray[] = {5, 10, 15, 20, 25, 30} // With normal for loop int count = 0; for( int i = 0; i < integerArray.length; i++){ int currentInteger = integerArray[i]; count += currentInteger; } // Now, with a for each loop int count = 0; for ( int currentInteger : integerArray ) { count += currentInteger; } Notice, we skipped a step in the second one because it will loop through with the first element of the array as the name currentInteger, and when it is done adding itself to count, it will go to the next element. We don't keep track of the size, we don't have the count variable, we don't have int i or i++, we simply manipulate the elements located within the array and do what we want. Now, of course under the hood Java is keeping track of where we are, and setting that variable to the next one on each rotation, but that's what syntactical sugar is all about. We focus on writing code, not boilerplate, and having a for each loop let's us worry about the actual logic, not correctly initializing a for loop and keeping track of indices. More on reddit.com
🌐 r/learnprogramming
7
1
November 2, 2015
How to access c:forEach index from the tag - Oracle Forums
I have a custom tag that alternates color of the row in the results tables based on the index of the collection that I am displaying. If the row is odd it is shown as gray, if it is even it is white.... More on forums.oracle.com
🌐 forums.oracle.com
April 5, 2004
How to get an index for current iteration of For Each
Hi all, I was wondering if there is a way to get the iteration number inside a for each activity (while it is running), other than using a counter. To be more clear, is there something like a current index for the for each activity, that i can query and use? A quick example in the attached ... More on forum.uipath.com
🌐 forum.uipath.com
1
2
December 20, 2018
How to add index for each record in an array of objects
You could add an int as an attribute for each object and set it equal to a static int in thr object class that increments for each object that is added More on reddit.com
🌐 r/javahelp
4
6
March 2, 2019
🌐
Baeldung
baeldung.com › home › java › core java › guide to the java foreach loop
Guide to the Java forEach Loop | Baeldung
June 17, 2025 - In Java, the Collection interface has Iterable as its super interface. This interface has a new API starting with Java 8: ... Simply put, the Javadoc of forEach states that it “performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.”
🌐
Baeldung
baeldung.com › home › java › java collections › how to access an iteration counter in a for each loop
How to Access an Iteration Counter in a For Each Loop | Baeldung
January 8, 2024 - Java's for each loop does not provide an iteration counter. There are a few ways we can iterate over both the item and its index.
🌐
Mkyong
mkyong.com › home › java8 › java 8 foreach print with index
Java 8 forEach print with Index - Mkyong.com
February 16, 2020 - Generate the index with IntStream.range. ... package com.mkyong.java8; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class JavaArrayWithIndex { public static void main(String[] args) { String[] names = {"Java", "Node", "JavaScript", "Rust", "Go"}; List<String> collect = IntStream.range(0, names.length) .mapToObj(index -> index + ":" + names[index]) .collect(Collectors.toList()); collect.forEach(System.out::println); } }
🌐
W3Schools
w3schools.com › java › java_foreach_loop.asp
Java For-Each Loop
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... There is also a "for-each" loop, which is used exclusively to loop through elements in an array (or other data structures):
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 272398 › what-is-for-each-loop-in-java-used-for
What is for each loop in java used for? | Sololearn: Learn to code for FREE!
March 23, 2017 - It means you're defining each element of that array as i. So you can access the elements using i instead of array [index]. foreach loop checks all elements of a collection from first to last, so you don't need the concept of index to access elements.
🌐
IONOS
ionos.com › digital guide › websites › web development › java for-each loop
How to use for-each loops in Java - IONOS
November 3, 2023 - In each iteration, you can perform automatic ma­nip­u­la­tions with common Java operators without having to write a separate statement for each element. In contrast to the for loop in Java, when you use the for-each Java loop, you don’t need to consider the index or size of the array.
🌐
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 - The enhanced foreach loop is a way of iterating through elements in arrays and collections in Java. It simplifies the traditional for loop syntax and eliminates the need for manual index management, making your code more readable and less prone ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › for-each-loop-in-java
For-Each Loop in Java - GeeksforGeeks
2 weeks ago - Explanation: The for-each loop does not provide access to the index of the current element. If we need the index for any reason (e.g., in a search operation), a traditional loop would be more appropriate.
🌐
Reddit
reddit.com › r/learnprogramming › [java] simple explanation of for each loops
r/learnprogramming on Reddit: [Java] Simple explanation of for each loops
November 2, 2015 -

Hi, every example I see of for each loops I see confuses me. I know for for loops it is: for(begining counter; do while counter is less, greater or equal to n; increase or decrease counter) . I was hoping someone could help explain how for each loops work and there applications instead of for loops. Sorry if this is a basic question, other answers I found in the java documentation and on stackoverflow didn't seem to help me.

Edit: I understand now! Thank you guys so much!

Top answer
1 of 2
2
Alright, so a forEach loop is simply some syntactical sugar. Something that makes a common problem or action easier to do. Easier can mean easier in terms of writing it, but also relates to code readability. Anyways, so a normal for loop takes the three conditions that you have stated: for(int i = 0; i < condition; i++ ) { } So I will have a counter here. A super common thing to use a for loop for, is to loop through the contents of an array. So: for(int i = 0; i < myArray.length; i++) { myElement = myArray[i]; } So, at the i-th index, get the element there, and then let's do something with it. But that's a lot of boilerplate ( read: Lot of typing to do something so basic and common ). So, the solution is to use the forEach loop. So we use the syntax: for ( myObject object : myArray ) { } This seems confusing at first, but break it down. All it is saying, in English is: I would like to loop through every element of myArray. During each loop, I would like the current element I am at to be referenced with the variable object which is of type myObject You are now allowed to work with this. But myObject and object are placeholders, and they aren't the easiest to understand. So I will write you a small program that will sum all of the numbers in an array. int integerArray[] = {5, 10, 15, 20, 25, 30} // With normal for loop int count = 0; for( int i = 0; i < integerArray.length; i++){ int currentInteger = integerArray[i]; count += currentInteger; } // Now, with a for each loop int count = 0; for ( int currentInteger : integerArray ) { count += currentInteger; } Notice, we skipped a step in the second one because it will loop through with the first element of the array as the name currentInteger, and when it is done adding itself to count, it will go to the next element. We don't keep track of the size, we don't have the count variable, we don't have int i or i++, we simply manipulate the elements located within the array and do what we want. Now, of course under the hood Java is keeping track of where we are, and setting that variable to the next one on each rotation, but that's what syntactical sugar is all about. We focus on writing code, not boilerplate, and having a for each loop let's us worry about the actual logic, not correctly initializing a for loop and keeping track of indices.
2 of 2
1
Say you have an array of things. Let's call it int[] myArray and let's say we put 1, 2, 3 inside of it. And you want to print each thing inside of myArray. So we start at the beginning of myArray and start walking towards the end. Each step we take, we take the number at that step, set it to some variable i and then print i. You could write a foreach loop like this: for (int i : myArray) { System.out.println(i); } What this does is it goes through myArray, starting at the beginning, and it: Sets i to the current element inside of myArray. Does the stuff in the body (the system.out.print...) So it basically does the stuff in the body for each thing inside of the array. First, i is equal to 1, and it prints 1. Then, i is equal to 2, and it prints 2. Finally, i is equal to 3, and it prints 3.
🌐
GeeksforGeeks
geeksforgeeks.org › java › iterate-through-list-in-java
Iterate through List in Java - GeeksforGeeks
July 23, 2025 - Iterating over a list can also be achieved using a while loop. The block of code inside the loop executes until the condition is true. A loop variable can be used as an index to access each element.
🌐
Medium
medium.com › javarevisited › i-need-an-index-with-this-list-iteration-method-1e339fd55ed7
I need an index with this List iteration method | by Donald Raab | Javarevisited | Medium
February 10, 2024 - Notice however, that with subList(), we lost access to the index in the call to forEach. There is no way to get access to this index back using subList. Eclipse Collections has a few internal iterators which are passed the element and index to the functional interface that is provided. This method will iterate from first element to last element passing each element and its index to the ObjectIntProcedure provided.
🌐
W3Docs
w3docs.com › java
Java, How do I get current index/key in "for each" loop
List<String> list = Arrays.asList("a", "b", "c"); ListIterator<String> iterator = list.listIterator(); while (iterator.hasNext()) { String item = iterator.next(); int index = iterator.previousIndex(); // Use index as the key here } ... Note that the for each loop is simply a shorthand for iterating through a collection and is not meant to provide access to the current index/key.
🌐
FavTutor
favtutor.com › blogs › foreach-java
For-Each Loop in Java (with Examples)
December 5, 2023 - By implementing the Iterable interface, our custom object seamlessly integrates with the for each loop, providing a clean and expressive way to iterate over its elements. Despite the simplification of iteration by the foreach loop, it introduces a significant limitation – it lacks support for the modification of elements during the iteration process. Attempting such modifications within the loop context may lead to encountering a ConcurrentModificationException. Let's illustrate this limitation through an example: import java.util.ArrayList; import java.util.List; public class ForEachModific
🌐
Oracle
forums.oracle.com › ords › apexds › post › how-to-access-c-foreach-index-from-the-tag-8877
How to access c:forEach index from the tag - Oracle Forums
April 5, 2004 - I have a custom tag that alternates color of the row in the results tables based on the index of the collection that I am displaying. If the row is odd it is shown as gray, if it is even it is white....
🌐
Codemia
codemia.io › knowledge-hub › path › java_8_foreach_with_index_duplicate
Java 8 forEach with index
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
🌐
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 - To loop through and print all the numbers in the array, we made use of a for-each loop: for(int number : even_numbers){...}. In the parenthesis for the loop, we created an integer variable called number which would be used to loop through the ...