elementAt

fun <T> Iterable<T>.elementAt(index: Int): T(source)

Returns an element at the given index or throws an IndexOutOfBoundsException 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.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
}

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

Returns an element at the given index or throws an IndexOutOfBoundsException 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.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
}

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

Returns an element at the given index or throws an IndexOutOfBoundsException 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.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
}