elementAtOrNull

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

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.elementAtOrNull(0), "1")
assertPrints(list.elementAtOrNull(2), "3")
assertPrints(list.elementAtOrNull(3), "null")

val emptyList = emptyList<Int>()
assertPrints(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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAtOrNull(0), "1")
assertPrints(list.elementAtOrNull(2), "3")
assertPrints(list.elementAtOrNull(3), "null")

val emptyList = emptyList<Int>()
assertPrints(emptyList.elementAtOrNull(0), "null") 
   //sampleEnd
}

inline 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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAtOrNull(0), "1")
assertPrints(list.elementAtOrNull(2), "3")
assertPrints(list.elementAtOrNull(3), "null")

val emptyList = emptyList<Int>()
assertPrints(emptyList.elementAtOrNull(0), "null") 
   //sampleEnd
}

inline fun UIntArray.elementAtOrNull(index: Int): UInt?
inline fun ULongArray.elementAtOrNull(index: Int): ULong?
inline fun UByteArray.elementAtOrNull(index: Int): UByte?
inline 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.

Since Kotlin

1.3

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAtOrNull(0), "1")
assertPrints(list.elementAtOrNull(2), "3")
assertPrints(list.elementAtOrNull(3), "null")

val emptyList = emptyList<Int>()
assertPrints(emptyList.elementAtOrNull(0), "null") 
   //sampleEnd
}