W3cubDocs

/Kotlin

lastIndex

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
val <T> Array<out T>.lastIndex: Int
val ByteArray.lastIndex: Int
val ShortArray.lastIndex: Int
val IntArray.lastIndex: Int
val LongArray.lastIndex: Int
val FloatArray.lastIndex: Int
val DoubleArray.lastIndex: Int
val BooleanArray.lastIndex: Int
val CharArray.lastIndex: Int
@ExperimentalUnsignedTypes inline val UIntArray.lastIndex: Int
@ExperimentalUnsignedTypes inline val ULongArray.lastIndex: Int
@ExperimentalUnsignedTypes inline val UByteArray.lastIndex: Int
@ExperimentalUnsignedTypes inline val UShortArray.lastIndex: Int

Returns the last valid index for the array.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
val <T> List<T>.lastIndex: Int

Returns the index of the last item in the list or -1 if the list is empty.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
println(emptyList<Any>().lastIndex) // -1
val list = listOf("a", "x", "y")
println(list.lastIndex) // 2
println(list[list.lastIndex]) // y
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/last-index.html