elementAtOrNull

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

inline fun <T> Array<out T>.elementAtOrNull(index: Int): T?(source)
fun main() {
    val fruits = arrayOf("Apple", "Banana", "Cherry")

    // Valid index
    val second = fruits.elementAtOrNull(1)   // "Banana"
    println("Element at index 1: $second")

    // Out‑of‑bounds index
    val outOfBounds = fruits.elementAtOrNull(5)   // null
    println("Element at index 5: $outOfBounds")
}

Source