take

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

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

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

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

(source)

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.