take
Returns a list containing first n elements.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val chars = ('a'..'z').toList()
assertPrints(chars.take(3), "[a, b, c]")
assertPrints(chars.takeWhile { it < 'f' }, "[a, b, c, d, e]")
assertPrints(chars.takeLast(2), "[y, z]")
assertPrints(chars.takeLastWhile { it > 'w' }, "[x, y, z]")
//sampleEnd
}
Throws
if n is negative.
Returns a list containing first n elements.
Since Kotlin
1.3
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val chars = ('a'..'z').toList()
assertPrints(chars.take(3), "[a, b, c]")
assertPrints(chars.takeWhile { it < 'f' }, "[a, b, c, d, e]")
assertPrints(chars.takeLast(2), "[y, z]")
assertPrints(chars.takeLastWhile { it > 'w' }, "[x, y, z]")
//sampleEnd
}
Throws
if n is negative.