Spec-Zone.ru › Kotlin 2

last

kotlin-stdlib/kotlin.ranges/last

Начиная с Kotlin: 1.7

fun IntProgression.last(): Int
fun LongProgression.last(): Long
fun CharProgression.last(): Char
fun UIntProgression.last(): UInt
fun ULongProgression.last(): ULong

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

Выбрасывает

NoSuchElementException

если прогрессия пуста.

Примеры

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4)
println(list.last()) // 4
println(list.last { it % 2 == 1 }) // 3
println(list.lastOrNull { it < 0 }) // null
// list.last { it < 0 } // will fail

val emptyList = emptyList<Int>()
println(emptyList.lastOrNull()) // null
// emptyList.last() // will fail 
   //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.ranges/last.html

Spec-Zone.ru

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