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 fun <T> Array<out T>.elementAt(index: Int): T(source)
expect fun ByteArray.elementAt(index: Int): Byte(source)
expect fun ShortArray.elementAt(index: Int): Short(source)
expect fun IntArray.elementAt(index: Int): Int(source)
expect fun LongArray.elementAt(index: Int): Long(source)
expect fun FloatArray.elementAt(index: Int): Float(source)
expect fun DoubleArray.elementAt(index: Int): Double(source)
expect fun BooleanArray.elementAt(index: Int): Boolean(source)
expect 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
}

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

Since Kotlin

1.3

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
}
actual fun <T> Array<out T>.elementAt(index: Int): T(source)
actual fun ByteArray.elementAt(index: Int): Byte(source)
actual fun ShortArray.elementAt(index: Int): Short(source)
actual fun IntArray.elementAt(index: Int): Int(source)
actual fun LongArray.elementAt(index: Int): Long(source)
actual fun FloatArray.elementAt(index: Int): Float(source)
actual fun DoubleArray.elementAt(index: Int): Double(source)
actual fun BooleanArray.elementAt(index: Int): Boolean(source)
actual 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.1

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
}

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

Since Kotlin

1.3

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
}
actual inline fun <T> Array<out T>.elementAt(index: Int): T(source)
actual inline fun ByteArray.elementAt(index: Int): Byte(source)
actual inline fun ShortArray.elementAt(index: Int): Short(source)
actual inline fun IntArray.elementAt(index: Int): Int(source)
actual inline fun LongArray.elementAt(index: Int): Long(source)
actual inline fun FloatArray.elementAt(index: Int): Float(source)
actual inline fun DoubleArray.elementAt(index: Int): Double(source)
actual inline fun BooleanArray.elementAt(index: Int): Boolean(source)
actual 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
}

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

Since Kotlin

1.3

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
}
actual inline fun <T> Array<out T>.elementAt(index: Int): T(source)
actual inline fun ByteArray.elementAt(index: Int): Byte(source)
actual inline fun ShortArray.elementAt(index: Int): Short(source)
actual inline fun IntArray.elementAt(index: Int): Int(source)
actual inline fun LongArray.elementAt(index: Int): Long(source)
actual inline fun FloatArray.elementAt(index: Int): Float(source)
actual inline fun DoubleArray.elementAt(index: Int): Double(source)
actual inline fun BooleanArray.elementAt(index: Int): Boolean(source)
actual 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.3

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
}
actual inline fun <T> Array<out T>.elementAt(index: Int): T(source)
actual inline fun ByteArray.elementAt(index: Int): Byte(source)
actual inline fun ShortArray.elementAt(index: Int): Short(source)
actual inline fun IntArray.elementAt(index: Int): Int(source)
actual inline fun LongArray.elementAt(index: Int): Long(source)
actual inline fun FloatArray.elementAt(index: Int): Float(source)
actual inline fun DoubleArray.elementAt(index: Int): Double(source)
actual inline fun BooleanArray.elementAt(index: Int): Boolean(source)
actual 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.8

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
}
actual inline fun <T> Array<out T>.elementAt(index: Int): T(source)
actual inline fun ByteArray.elementAt(index: Int): Byte(source)
actual inline fun ShortArray.elementAt(index: Int): Short(source)
actual inline fun IntArray.elementAt(index: Int): Int(source)
actual inline fun LongArray.elementAt(index: Int): Long(source)
actual inline fun FloatArray.elementAt(index: Int): Float(source)
actual inline fun DoubleArray.elementAt(index: Int): Double(source)
actual inline fun BooleanArray.elementAt(index: Int): Boolean(source)
actual 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.8

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
}