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