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
}

相关方法

相关推荐
黑码哥8 分钟前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
亓才孓19 分钟前
[JDBC]元数据
android
独行soc31 分钟前
2026年渗透测试面试题总结-17(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
金融RPA机器人丨实在智能39 分钟前
Android Studio开发App项目进入AI深水区:实在智能Agent引领无代码交互革命
android·人工智能·ai·android studio
科技块儿40 分钟前
利用IP查询在智慧城市交通信号系统中的应用探索
android·tcp/ip·智慧城市
独行soc1 小时前
2026年渗透测试面试题总结-18(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
王码码20352 小时前
Flutter for OpenHarmony 实战之基础组件:第二十七篇 BottomSheet — 动态底部弹窗与底部栏菜单
android·flutter·harmonyos
2501_915106322 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
vistaup2 小时前
OKHTTP 默认构建包含 android 4.4 的TLS 1.2 以及设备时间不对兼容
android·okhttp
常利兵2 小时前
ButterKnife在Android 35 + Gradle 8.+环境下的适配困境与现代化迁移指南
android