lastIndex
Returns the last valid index for the array.
Since Kotlin
1.0Returns the last valid index for the array.
Since Kotlin
1.3Returns the index of the last item in the list or -1 if the list is empty.
Returns the last valid index for the array.
Returns the last valid index for the array.
Returns the index of the last item in the list or -1 if the list is empty.
import kotlin.test.* fun main() { //sampleStart println(emptyList<Any>().lastIndex) // -1 val list = listOf("a", "x", "y") println(list.lastIndex) // 2 println(list[list.lastIndex]) // y //sampleEnd }
xxxxxxxxxx
println(emptyList<Any>().lastIndex) // -1
val list = listOf("a", "x", "y")
println(list.lastIndex) // 2
println(list[list.lastIndex]) // y
Thanks for your feedback!