elementAtOrNull
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 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
}
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
}
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
}