takeLast

Common
JVM
JS
Native
1.0
fun <T> Array<out T>.takeLast(n: Int): List<T>
(source)
fun ByteArray.takeLast(n: Int): List<Byte>
(source)
fun ShortArray.takeLast(n: Int): List<Short>
(source)
fun IntArray.takeLast(n: Int): List<Int>
(source)
fun LongArray.takeLast(n: Int): List<Long>
(source)
fun FloatArray.takeLast(n: Int): List<Float>
(source)
fun DoubleArray.takeLast(n: Int): List<Double>
(source)
fun BooleanArray.takeLast(n: Int): List<Boolean>
(source)
fun CharArray.takeLast(n: Int): List<Char>
(source)
fun <T> List<T>.takeLast(n: Int): List<T>
(source)
@ExperimentalUnsignedTypes fun UIntArray.takeLast(
    n: Int
): List<UInt>

(source)
@ExperimentalUnsignedTypes fun ULongArray.takeLast(
    n: Int
): List<ULong>

(source)
@ExperimentalUnsignedTypes fun UByteArray.takeLast(
    n: Int
): List<UByte>

(source)
@ExperimentalUnsignedTypes fun UShortArray.takeLast(
    n: Int
): List<UShort>

(source)

Returns a list containing last n elements.

import kotlin.test.*

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

Exceptions

IllegalArgumentException - if n is negative.