How to get the current index in for each Kotlin (#9710)

This commit is contained in:
Mona Mohamadinia 2020-07-21 19:24:20 +04:30 committed by GitHub
parent 2ddbdb5b4e
commit de2ad1bc40
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package com.baeldung.index
fun main() {
// Index only
val colors = listOf("Red", "Green", "Blue")
for (i in colors.indices) {
println(colors[i])
}
val colorArray = arrayOf("Red", "Green", "Blue")
for (i in colorArray.indices) {
println(colorArray[i])
}
(0 until colors.size).forEach { println(colors[it]) }
for (i in 0 until colors.size) {
println(colors[i])
}
// Index and Value
colors.forEachIndexed { i, v -> println("The value for index $i is $v") }
for (indexedValue in colors.withIndex()) {
println("The value for index ${indexedValue.index} is ${indexedValue.value}")
}
for ((i, v) in colors.withIndex()) {
println("The value for index $i is $v")
}
colors.filterIndexed { i, _ -> i % 2 == 0 }
colors.filterIndexed { _, v -> v == "RED" }
}