
1. Overview
In this article, we will learn kotlin foreach with index example. The for loop in Kotlin is equivalent to the foreach loop in other languages.
2. Kotlin for with index example
The syntax of the for loop is:
for (element in collection) print(element) or for (element in collection) { print(element) ... }
Let’s take a simple for loop example in Kotlin. In this example, we have a list of square numbers. Then using the for loop, we are printing each square number.
fun main() { val squareNumbers = arrayOf(0, 1, 4, 9, 16, 25, 36) for (element in squareNumbers) { println(element) } } Result: 0 1 4 9 16 25 36
You can also print the list index of each square number using any of the following ways.
2.1. Kotlin foreach index using indices
The indices function returns the range of valid indices of the collection. In the below example, the valid range of squareNumbers
index is 0 to 6.
Therefore, the indices function will print the range as 0..6
.
fun main() { val squareNumbers = arrayOf(0, 1, 4, 9, 16, 25, 36) println(squareNumbers.indices) } Result: 0..6
If you want to iterate through an array or a list with an index, you can use the indices function:
fun main() { val squareNumbers = arrayOf(0, 1, 4, 9, 16, 25, 36) for (i in squareNumbers.indices) { println("Index: " + i + " square number: " + squareNumbers[i]) } } Result: Index: 0 square number: 0 Index: 1 square number: 1 Index: 2 square number: 4 Index: 3 square number: 9 Index: 4 square number: 16 Index: 5 square number: 25 Index: 6 square number: 36
2.2. index using withIndex
Similar to indices, you can also use withIndex function to get the index inside the for loop.
fun main() { val squareNumbers = arrayOf(0, 1, 4, 9, 16, 25, 36) for ((index, value) in squareNumbers.withIndex()) { println("the element at $index is $value") } } Result: the element at 0 is 0 the element at 1 is 1 the element at 2 is 4 the element at 3 is 9 the element at 4 is 16 the element at 5 is 25 the element at 6 is 36
The withIndex function wraps each element into an IndexedValue element. Then, provides an Iterator to iterate the IndexedValue elements. So, each IndexedValue contains the index of the element and the element itself.
In the above example, (index, value) in for loop represents the IndexedValue.
The IndexedValue
is a data class that contains the index and the corresponding element.
IndexedValue(index: Int, value: T)
2.2. Index using forEachIndexed
Alternatively, you can also use forEachIndexed function to iterate through the elements of a collection with its index.
In the below code, we are using forEachIndexed to iterate through each element of squareNumbers
.
fun main() { val squareNumbers = arrayOf(0, 1, 4, 9, 16, 25, 36) squareNumbers.forEachIndexed{index, item -> println("index = $index, item = $item ") } }
Besides the index, you can also perform an action on the element like updating the item.
Let’s take that we have a Toy
class with name
, type
and isInStock
variables. In the below forEachIndexed loop, we are checking the Slider
toy and updating its stock status to false
.
It will work fine and therefore forEachIndexed function allows you to alter the element inside the loop.
fun main() { val toys = arrayOf(Toy("Car", "Indoor", true), Toy("Slider", "Outdoor", true), Toy("Rider", "Outdoor", true)) toys.forEachIndexed{index, item -> if (item.name == "Slider") { item.isInStock = false } } for(toy in toys) { println(toy) } } data class Toy(val name: String, val type: String, var isInStock: Boolean)
3. Conclusion
To sum up, we have discussed the kotlin foreach with index example. We have also seen each of the functions indices, withIndex or forEachIndexed that helps to iterate a collection with index.
See our article on when expression if you are interested.
Pingback: Kotlin loop until: excludes the end element - TedBlob