Spec-Zone.ru › Kotlin 2

lastIndex

kotlin-stdlib/kotlin.collections/lastIndex

Начиная с Kotlin: 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
val UIntArray.lastIndex: Int
@ExperimentalUnsignedTypes
val ULongArray.lastIndex: Int
@ExperimentalUnsignedTypes
val UByteArray.lastIndex: Int
@ExperimentalUnsignedTypes
val UShortArray.lastIndex: Int

Возвращает последний допустимый индекс массива.


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

Возвращает индекс последнего элемента списка или -1, если список пуст.

Примеры

import kotlin.math.*
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
}

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

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API