get Or Null
Returns an element at the given index or null
if the index is out of bounds of this array.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val list = listOf(1, 2, 3)
assertPrints(list.getOrNull(0), "1")
assertPrints(list.getOrNull(2), "3")
assertPrints(list.getOrNull(3), "null")
val emptyList = emptyList<Int>()
assertPrints(emptyList.getOrNull(0), "null")
//sampleEnd
}
Returns an element at the given index or null
if the index is out of bounds of this list.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val list = listOf(1, 2, 3)
assertPrints(list.getOrNull(0), "1")
assertPrints(list.getOrNull(2), "3")
assertPrints(list.getOrNull(3), "null")
val emptyList = emptyList<Int>()
assertPrints(emptyList.getOrNull(0), "null")
//sampleEnd
}
Returns an element at the given index or null
if the index is out of bounds of this array.
Since Kotlin
1.3
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val list = listOf(1, 2, 3)
assertPrints(list.getOrNull(0), "1")
assertPrints(list.getOrNull(2), "3")
assertPrints(list.getOrNull(3), "null")
val emptyList = emptyList<Int>()
assertPrints(emptyList.getOrNull(0), "null")
//sampleEnd
}