W3cubDocs

/Kotlin

elementAtOrNull

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> Array<out T>.elementAtOrNull(index: Int): T?
fun ByteArray.elementAtOrNull(index: Int): Byte?
fun ShortArray.elementAtOrNull(index: Int): Short?
fun IntArray.elementAtOrNull(index: Int): Int?
fun LongArray.elementAtOrNull(index: Int): Long?
fun FloatArray.elementAtOrNull(index: Int): Float?
fun DoubleArray.elementAtOrNull(index: Int): Double?
fun BooleanArray.elementAtOrNull(index: Int): Boolean?
fun CharArray.elementAtOrNull(index: Int): Char?
@ExperimentalUnsignedTypes fun UIntArray.elementAtOrNull(
    index: Int
): UInt?
@ExperimentalUnsignedTypes fun ULongArray.elementAtOrNull(
    index: Int
): ULong?
@ExperimentalUnsignedTypes fun UByteArray.elementAtOrNull(
    index: Int
): UByte?
@ExperimentalUnsignedTypes fun UShortArray.elementAtOrNull(
    index: Int
): UShort?

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
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> Iterable<T>.elementAtOrNull(index: Int): T?

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
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> List<T>.elementAtOrNull(index: Int): T?

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
}

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/element-at-or-null.html