elementAt所在位置是kotlin.collections.elementAt,其相关用法介绍如下。

用法一

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

返回给定 index 处的元素,如果 index 超出此数组的范围,则抛出 IndexOutOfBoundsException

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//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
}

输出:

1
3

用法二

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

返回给定 index 处的元素,如果 index 超出此集合的范围,则抛出 IndexOutOfBoundsException

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//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
}

输出:

1
3

用法三

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

返回给定 index 处的元素,如果 index 超出此列表的范围,则抛出 IndexOutOfBoundsException

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//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
}

输出:

1
3