How to get the index in a for each loop? I want to print numbers for every second iteration
For example
for (value in collection) { if (iteration_no %
Use indices
indices
for (i in array.indices) { print(array[i]) }
If you want value as well as index Use withIndex()
withIndex()
for ((index, value) in array.withIndex()) { println("the element at $index is $value") }
Reference: Control-flow in kotlin