take
fun <T> Array<out T>.take(n: Int): List<T>
fun ShortArray.take(n: Int): List<Short>
fun FloatArray.take(n: Int): List<Float>
fun DoubleArray.take(n: Int): List<Double>
fun BooleanArray.take(n: Int): List<Boolean>
@ExperimentalUnsignedTypes fun ULongArray.take(
n: Int
): List<ULong>
@ExperimentalUnsignedTypes fun UByteArray.take(
n: Int
): List<UByte>
@ExperimentalUnsignedTypes fun UShortArray.take(
n: Int
): List<UShort>
Returns a list containing first 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.