lastIndex

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

Returns the last valid index for the array.


val UIntArray.lastIndex: Int
val ULongArray.lastIndex: Int
val UByteArray.lastIndex: Int
val UShortArray.lastIndex: Int

Returns the last valid index for the array.

Since Kotlin

1.3

val <T> List<T>.lastIndex: Int

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   assertPrints(emptyList<Any>().lastIndex, "-1")
val list = listOf("a", "x", "y")
assertPrints(list.lastIndex, "2")
assertPrints(list[list.lastIndex], "y") 
   //sampleEnd
}