elementAt

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
}

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
}

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

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

@ExperimentalUnsignedTypes
actual inline fun UIntArray.elementAt(index: Int): UInt
@ExperimentalUnsignedTypes
actual inline fun ULongArray.elementAt(index: Int): ULong
@ExperimentalUnsignedTypes
actual inline fun UByteArray.elementAt(index: Int): UByte
@ExperimentalUnsignedTypes
actual 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
}
actual fun <T> Array<out T>.elementAt(index: Int): T
actual fun ByteArray.elementAt(index: Int): Byte
actual fun ShortArray.elementAt(index: Int): Short
actual fun IntArray.elementAt(index: Int): Int
actual fun LongArray.elementAt(index: Int): Long
actual fun FloatArray.elementAt(index: Int): Float
actual fun DoubleArray.elementAt(index: Int): Double
actual fun BooleanArray.elementAt(index: Int): Boolean
actual 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
}

@ExperimentalUnsignedTypes
actual fun UIntArray.elementAt(index: Int): UInt
@ExperimentalUnsignedTypes
actual fun ULongArray.elementAt(index: Int): ULong
@ExperimentalUnsignedTypes
actual fun UByteArray.elementAt(index: Int): UByte
@ExperimentalUnsignedTypes
actual 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
}
actual inline fun <T> Array<out T>.elementAt(index: Int): T
actual inline fun ByteArray.elementAt(index: Int): Byte
actual inline fun ShortArray.elementAt(index: Int): Short
actual inline fun IntArray.elementAt(index: Int): Int
actual inline fun LongArray.elementAt(index: Int): Long
actual inline fun FloatArray.elementAt(index: Int): Float
actual inline fun DoubleArray.elementAt(index: Int): Double
actual inline fun BooleanArray.elementAt(index: Int): Boolean
actual 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
}

@ExperimentalUnsignedTypes
actual inline fun UIntArray.elementAt(index: Int): UInt
@ExperimentalUnsignedTypes
actual inline fun ULongArray.elementAt(index: Int): ULong
@ExperimentalUnsignedTypes
actual inline fun UByteArray.elementAt(index: Int): UByte
@ExperimentalUnsignedTypes
actual 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
}