elementAtOrNull

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

(source)
@ExperimentalUnsignedTypes fun ULongArray.elementAtOrNull(
    index: Int
): ULong?

(source)
@ExperimentalUnsignedTypes fun UByteArray.elementAtOrNull(
    index: Int
): UByte?

(source)
@ExperimentalUnsignedTypes fun UShortArray.elementAtOrNull(
    index: Int
): UShort?

(source)

Returns an element at the given index or null if the index is out of bounds of this array.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3)
println(list.elementAtOrNull(0)) // 1
println(list.elementAtOrNull(2)) // 3
println(list.elementAtOrNull(3)) // null

val emptyList = emptyList<Int>()
println(emptyList.elementAtOrNull(0)) // null
//sampleEnd
}
Common
JVM
JS
Native
1.0
fun <T> Iterable<T>.elementAtOrNull(index: Int): T?
(source)

Returns an element at the given index or null if the index is out of bounds of this collection.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3)
println(list.elementAtOrNull(0)) // 1
println(list.elementAtOrNull(2)) // 3
println(list.elementAtOrNull(3)) // null

val emptyList = emptyList<Int>()
println(emptyList.elementAtOrNull(0)) // null
//sampleEnd
}
Common
JVM
JS
Native
1.0
fun <T> List<T>.elementAtOrNull(index: Int): T?
(source)

Returns an element at the given index or null if the index is out of bounds of this list.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3)
println(list.elementAtOrNull(0)) // 1
println(list.elementAtOrNull(2)) // 3
println(list.elementAtOrNull(3)) // null

val emptyList = emptyList<Int>()
println(emptyList.elementAtOrNull(0)) // null
//sampleEnd
}