elementAt

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}
Common
inline 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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}
Common
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}
inline fun <T> Array<out T>.elementAt(index: Int): T
inline fun ByteArray.elementAt(index: Int): Byte
inline fun ShortArray.elementAt(index: Int): Short
inline fun IntArray.elementAt(index: Int): Int
inline fun LongArray.elementAt(index: Int): Long
inline fun FloatArray.elementAt(index: Int): Float
inline fun DoubleArray.elementAt(index: Int): Double
inline fun BooleanArray.elementAt(index: Int): Boolean
inline 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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}
JS
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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}
Native
inline fun <T> Array<out T>.elementAt(index: Int): T
inline fun ByteArray.elementAt(index: Int): Byte
inline fun ShortArray.elementAt(index: Int): Short
inline fun IntArray.elementAt(index: Int): Int
inline fun LongArray.elementAt(index: Int): Long
inline fun FloatArray.elementAt(index: Int): Float
inline fun DoubleArray.elementAt(index: Int): Double
inline fun BooleanArray.elementAt(index: Int): Boolean
inline 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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}
Common
fun UIntArray.elementAt(index: Int): UInt
fun ULongArray.elementAt(index: Int): ULong
fun UByteArray.elementAt(index: Int): UByte
fun UShortArray.elementAt(index: Int): UShort

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 samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}
@ExperimentalUnsignedTypes
inline fun UIntArray.elementAt(index: Int): UInt
@ExperimentalUnsignedTypes
inline fun ULongArray.elementAt(index: Int): ULong
@ExperimentalUnsignedTypes
inline fun UByteArray.elementAt(index: Int): UByte
@ExperimentalUnsignedTypes
inline fun UShortArray.elementAt(index: Int): UShort

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 samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}
JS
@ExperimentalUnsignedTypes
fun UIntArray.elementAt(index: Int): UInt
@ExperimentalUnsignedTypes
fun ULongArray.elementAt(index: Int): ULong
@ExperimentalUnsignedTypes
fun UByteArray.elementAt(index: Int): UByte
@ExperimentalUnsignedTypes
fun UShortArray.elementAt(index: Int): UShort

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 samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}
Native
@ExperimentalUnsignedTypes
inline fun UIntArray.elementAt(index: Int): UInt
@ExperimentalUnsignedTypes
inline fun ULongArray.elementAt(index: Int): ULong
@ExperimentalUnsignedTypes
inline fun UByteArray.elementAt(index: Int): UByte
@ExperimentalUnsignedTypes
inline fun UShortArray.elementAt(index: Int): UShort

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 samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
assertPrints(list.elementAt(0), "1")
assertPrints(list.elementAt(2), "3")
assertFailsWith<IndexOutOfBoundsException> { list.elementAt(3) }

val emptyList = emptyList<Int>()
assertFailsWith<IndexOutOfBoundsException> { emptyList.elementAt(0) } 
   //sampleEnd
}