elementAtOrNull

inline fun <T> Array<out T>.elementAtOrNull(index: Int): T?(source)
inline fun ByteArray.elementAtOrNull(index: Int): Byte?(source)
inline fun ShortArray.elementAtOrNull(index: Int): Short?(source)
inline fun IntArray.elementAtOrNull(index: Int): Int?(source)
inline fun LongArray.elementAtOrNull(index: Int): Long?(source)
inline fun FloatArray.elementAtOrNull(index: Int): Float?(source)
inline fun CharArray.elementAtOrNull(index: Int): Char?(source)

Returns an element at the given index or null if the index is out of bounds of this array.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //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?(source)

Returns an element at the given index or null if the index is out of bounds of this collection.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //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
}

inline fun <T> List<T>.elementAtOrNull(index: Int): T?(source)

Returns an element at the given index or null if the index is out of bounds of this list.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //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
}