Find current index in a for-each loop in Java
This post will discuss how to find the current index in a for-each loop in Java.
The for-each loop simplifies the code by hiding the complexity behind the iterator() method. There is no direct provision to access the index of the current element with for-each construct, since it is used to loop over an Iterable, and not all Iterables actually have an index. This post provides an overview of some of the available alternatives to accomplish this.
1. Maintain a counter
A simple solution is to maintain an explicit counter, starting with 0, and increment the counter by 1 in each iteration of the loop. Here’s how the code would look like:
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { int[] arr = {4, 2, 5, 4, 1}; int index = 0; for (int i : arr) { System.out.printf("Index: %d, Element: %d\n", index++, i); } } } |
Output:
Index: 0, Element: 4
Index: 1, Element: 2
Index: 2, Element: 5
Index: 3, Element: 4
Index: 4, Element: 1
If there is no special reason to use the for-each loop, you should use a regular for loop to easily get the index for each element.
|
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { int[] arr = {4, 2, 5, 4, 1}; for(int i = 0; i < arr.length; i++) { System.out.printf("Index: %d, Element: %d\n", i, arr[i]); } } } |
Output:
Index: 0, Element: 4
Index: 1, Element: 2
Index: 2, Element: 5
Index: 3, Element: 4
Index: 4, Element: 1
2. Using AtomicInteger
We know that the variables used inside a lambda expression must be final or effectively final, hence a primitive integer variable cannot be used with a lambda expression. If you prefer Java 8 forEach() over the classical and enhanced for-loop, you can keep track of the index in an AtomicInteger.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; import java.util.concurrent.atomic.AtomicInteger; public class Main { public static void main(String[] args) { int[] arr = {4, 2, 5, 4, 1}; AtomicInteger indexHolder = new AtomicInteger(); Arrays.stream(arr).forEach(i -> { System.out.printf("Index: %d, Element: %d\n", indexHolder.getAndIncrement(), i); }); } } |
Output:
Index: 0, Element: 4
Index: 1, Element: 2
Index: 2, Element: 5
Index: 3, Element: 4
Index: 4, Element: 1
Alternatively, you can just iterate over the indices of the elements using an IntStream, as shown below:
|
1 2 3 4 5 6 7 8 9 10 |
import java.util.stream.IntStream; public class Main { public static void main(String[] args) { int[] arr = {4, 2, 5, 4, 1}; IntStream.range(0, arr.length) .forEach(i -> System.out.printf("Index: %d, Element: %d\n", i, arr[i])); } } |
Output:
Index: 0, Element: 4
Index: 1, Element: 2
Index: 2, Element: 5
Index: 3, Element: 4
Index: 4, Element: 1
3. Using ListIterator
Finally, you can use the iterator returned by the listIterator() method, which has the nextIndex() method. It returns the index of the element that would be returned by a subsequent call to the next() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.List; import java.util.ListIterator; public class Main { public static void main(String[] args) { List<Integer> values = List.of(4, 2, 5, 4, 1); ListIterator<Integer> it = values.listIterator(); while (it.hasNext()) { System.out.printf("Index: %d, Element: %d\n", it.nextIndex(), it.next()); } } } |
Output:
Index: 0, Element: 4
Index: 1, Element: 2
Index: 2, Element: 5
Index: 3, Element: 4
Index: 4, Element: 1
That’s all about finding the current index in a for-each loop in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)