Platform and version requirements: JVM (1.0), JS (1.0), Native (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>
@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>

Returns a list containing last n elements.

import kotlin.test.*

fun main(args: Array<String>) {
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]


IllegalArgumentException - if n is negative.

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.