Java doesn’t have a direct enumerate function like Python, but you can achieve similar functionality. Using a simple for loop:
String[] fruits = {“Apple”, “Banana”, “Cherry”};
for (int i = 0; i < fruits.length; i++) {
System.out.println("Index: " + i + ", Value: " + fruits[i]);
}
For lists, use IntStream:
IntStream.range(0, fruits.size()).forEach(i ->
System.out.println(“Index: ” + i + “, Value: ” + fruits.get(i))
);
Both methods provide the index and value while iterating through the collection.