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
}

相关方法

相关推荐
二川bro8 小时前
字符串特性解析:Python不可变性引发的错误
android·开发语言·python
2501_937154938 小时前
酷秒神马 9.0 2025 版:微服务架构
android·源码·源代码管理·机顶盒
我又来搬代码了9 小时前
【Android】【Compose】Compose知识点复习(一)
android·前端·kotlin·android studio
fatiaozhang95279 小时前
九联UNP-SJA8-国科GK6323V100C-2+8G-安卓9.0-原厂强刷固件包-可救砖及开ADB教程
android·adb·电视盒子·刷机固件·机顶盒刷机·机顶盒刷机固件·九联unp-sja8
似霰9 小时前
传统 Hal 开发笔记4----实现 Hal 模块
android·hal
Android出海9 小时前
Google Play正式出手整治后台耗电应用
android·新媒体运营·产品运营·流量运营·用户运营
Winter_Sun灬9 小时前
CentOS7 交叉编译 ACE+TAO-6.5.13 安卓 arm64-v8a 静态库
android·ace
Digitally9 小时前
4种有效方法:如何将音乐从Mac传输到Android
android
CheungChunChiu9 小时前
Android 多媒体体系完整总结
android
晓131310 小时前
SQL篇——【MySQL篇:运维】高可用架构搭建(主从、读写分离、分库分表)
android·数据库·mysql