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.

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.

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.

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.

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
}

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

@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 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
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 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
}

@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 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
}