Kotlin component2用法及代码示例

本文方法及代码示例基于Kotlin 2.1.20 Released

component2 所在包 kotlin.collections.component2,其相关用法介绍如下:

用法一

kotlin 复制代码
operator fun <T> Array<out T>.component2(): T

operator fun ByteArray.component2(): Byte

operator fun ShortArray.component2(): Short

operator fun IntArray.component2(): Int

operator fun LongArray.component2(): Long

operator fun FloatArray.component2(): Float

operator fun DoubleArray.component2(): Double

operator fun BooleanArray.component2(): Boolean

operator fun CharArray.component2(): Char

@ExperimentalUnsignedTypes operator fun UIntArray.component2(): UInt

@ExperimentalUnsignedTypes operator fun ULongArray.component2(): ULong

@ExperimentalUnsignedTypes operator fun UByteArray.component2(): UByte

@ExperimentalUnsignedTypes operator fun UShortArray.component2(): UShort

从数组中返回第二个 element

如果此数组的大小小于 2,则抛出 IndexOutOfBoundsException,除非在 Kotlin/JS 中未指定行为。

用法二

kotlin 复制代码
operator fun <T> List<T>.component2(): T

从列表中返回第二个 element

如果此列表的大小小于 2,则抛出 IndexOutOfBoundsException

用法三

kotlin 复制代码
operator fun <K, V> Entry<K, V>.component2(): V

返回映射条目的值组件。

此方法允许在使用Map时使用解构声明,例如:

kotlin 复制代码
for ((key, value) in map) {
    // do something with the key and the value
}

相关方法

相关推荐
SharpCJ9 小时前
Android 开发者为什么必须掌握 AI 能力?端侧视角下的技术变革
android·ai·aigc
_李小白10 小时前
【OSG学习笔记】Day 38: TextureVisitor(纹理访问器)
android·笔记·学习
JJay.10 小时前
Kotlin 高阶函数学习指南
android·开发语言·kotlin
android_cai_niao10 小时前
kotlin中的when
kotlin·when
渔舟小调10 小时前
后端框架选型:为什么选Kotlin + Spring Boot
kotlin·idea
jinanwuhuaguo10 小时前
截止到4月8日,OpenClaw 2026年4月更新深度解读剖析:从“能力回归”到“信任内建”的范式跃迁
android·开发语言·人工智能·深度学习·kotlin
JJay.10 小时前
Android Kotlin 协程使用指南
android·开发语言·kotlin
BLUcoding11 小时前
Android 布局介绍
android
summerkissyou198711 小时前
android-蓝牙-状态和协议值总结及监听例子
android·蓝牙
徒 花11 小时前
数据库知识复习05
android·数据库