W3cubDocs

/Kotlin

takeLast

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>

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
}

© 2010–2018 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/take-last.html