Avatar ·

How to get element index in forEach loop in Kotlin?

Hello! I'm new to Kotlin, transitioning from Java. In Java I used to use a regular for loop with index: ```java for (int i = 0; i < list.size(); i++) { System.out.println("Index: " + i + ", Value: " + list.get(i)); } ``` But in Kotlin I see that everyone recommends using forEach: ```kotlin list.forEach { item -> println(item) } ``` The problem is that I often need to know not only the element value, but also its index (position) in the list. For example, I'm making a task list in Android and want to show the sequence number of each task. Or I want to check if an element is the last in the list. I tried doing this: ```kotlin var index = 0 list.forEach { item -> println("$index: $item") index++ } ``` But this looks hacky and unreliable. If