elementAt

Common
JVM
JS
Native
1.0
fun <T> Array<out T>.elementAt(index: Int): T
fun ByteArray.elementAt(index: Int): Byte
fun ShortArray.elementAt(index: Int): Short
fun IntArray.elementAt(index: Int): Int
fun LongArray.elementAt(index: Int): Long
fun FloatArray.elementAt(index: Int): Float
fun DoubleArray.elementAt(index: Int): Double
fun BooleanArray.elementAt(index: Int): Boolean
fun CharArray.elementAt(index: Int): Char

Returns an element at the given index or throws an IndexOutOfBoundsException 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.elementAt(0)) // 1
println(list.elementAt(2)) // 3
// list.elementAt(3) // will fail with IndexOutOfBoundsException

val emptyList = emptyList<Int>()
// emptyList.elementAt(0) // will fail with IndexOutOfBoundsException
//sampleEnd
}
Common
JVM
JS
Native
1.0
fun <T> Iterable<T>.elementAt(index: Int): T

Returns an element at the given index or throws an IndexOutOfBoundsException 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.elementAt(0)) // 1
println(list.elementAt(2)) // 3
// list.elementAt(3) // will fail with IndexOutOfBoundsException

val emptyList = emptyList<Int>()
// emptyList.elementAt(0) // will fail with IndexOutOfBoundsException
//sampleEnd
}
Common
JVM
JS
Native
1.0
fun <T> List<T>.elementAt(index: Int): T

Returns an element at the given index or throws an IndexOutOfBoundsException 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.elementAt(0)) // 1
println(list.elementAt(2)) // 3
// list.elementAt(3) // will fail with IndexOutOfBoundsException

val emptyList = emptyList<Int>()
// emptyList.elementAt(0) // will fail with IndexOutOfBoundsException
//sampleEnd
}