lastIndex

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

Returns the last valid index for the array.

Common
JVM
JS
Native
1.0
val <T> List<T>.lastIndex: Int
(source)

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
}