Spec-Zone.ru › Kotlin 2

takeLast

kotlin-stdlib/kotlin.collections/takeLast

Начиная с Kotlin: 1.0

fun <T> Array<out T>.takeLast(n: Int): List<T>
fun ByteArray.takeLast(n: Int): List<Byte>
fun ShortArray.takeLast(n: Int): List<Short>
fun IntArray.takeLast(n: Int): List<Int>
fun LongArray.takeLast(n: Int): List<Long>
fun FloatArray.takeLast(n: Int): List<Float>
fun DoubleArray.takeLast(n: Int): List<Double>
fun BooleanArray.takeLast(n: Int): List<Boolean>
fun CharArray.takeLast(n: Int): List<Char>
fun <T> List<T>.takeLast(n: Int): List<T>

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

Исключения

IllegalArgumentException

если n отрицательно.

Примеры

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

fun main() { 
   //sampleStart 
   val chars = ('a'..'z').toList()
println(chars.take(3)) // [a, b, c]
println(chars.takeWhile { it < 'f' }) // [a, b, c, d, e]
println(chars.takeLast(2)) // [y, z]
println(chars.takeLastWhile { it > 'w' }) // [x, y, z] 
   //sampleEnd
}

@ExperimentalUnsignedTypes
fun UIntArray.takeLast(n: Int): List<UInt>
@ExperimentalUnsignedTypes
fun ULongArray.takeLast(n: Int): List<ULong>
@ExperimentalUnsignedTypes
fun UByteArray.takeLast(n: Int): List<UByte>
@ExperimentalUnsignedTypes
fun UShortArray.takeLast(n: Int): List<UShort>

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

Исключения

IllegalArgumentException

если n отрицательно.

Примеры

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

fun main() { 
   //sampleStart 
   val chars = ('a'..'z').toList()
println(chars.take(3)) // [a, b, c]
println(chars.takeWhile { it < 'f' }) // [a, b, c, d, e]
println(chars.takeLast(2)) // [y, z]
println(chars.takeLastWhile { it > 'w' }) // [x, y, z] 
   //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/take-last.html

Spec-Zone.ru

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