takeLast

fun <T> Array<out T>.takeLast(n: Int): List<T>(source)
fun <T> List<T>.takeLast(n: Int): List<T>(source)

Returns a list containing last n elements.

Since Kotlin

1.0

Throws

Samples

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
}

Returns a list containing last n elements.

Since Kotlin

1.3

Throws

Samples

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
}