fun <T> Array<out T>.getOrNull(index: Int): T?
fun ByteArray.getOrNull(index: Int): Byte?
fun ShortArray.getOrNull(index: Int): Short?
fun IntArray.getOrNull(index: Int): Int?
fun LongArray.getOrNull(index: Int): Long?
fun FloatArray.getOrNull(index: Int): Float?
fun DoubleArray.getOrNull(index: Int): Double?
fun BooleanArray.getOrNull(index: Int): Boolean?
fun CharArray.getOrNull(index: Int): Char?
@ExperimentalUnsignedTypes fun UIntArray.getOrNull( index: Int ): UInt?
@ExperimentalUnsignedTypes fun ULongArray.getOrNull( index: Int ): ULong?
@ExperimentalUnsignedTypes fun UByteArray.getOrNull( index: Int ): UByte?
@ExperimentalUnsignedTypes fun UShortArray.getOrNull( 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.getOrNull(0)) // 1
println(list.getOrNull(2)) // 3
println(list.getOrNull(3)) // null
val emptyList = emptyList<Int>()
println(emptyList.getOrNull(0)) // null
//sampleEnd
}
fun <T> List<T>.getOrNull(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.getOrNull(0)) // 1
println(list.getOrNull(2)) // 3
println(list.getOrNull(3)) // null
val emptyList = emptyList<Int>()
println(emptyList.getOrNull(0)) // null
//sampleEnd
}
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-null.html