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
}

相关方法

相关推荐
曾经的三心草1 小时前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器
Jerry2 小时前
Compose 设置文字样式
android
飞猿_SIR2 小时前
android定制系统完全解除应用安装限制
android
索迪迈科技3 小时前
影视APP源码 SK影视 安卓+苹果双端APP 反编译详细视频教程+源码
android·影视app源码·sk影视
孔丘闻言3 小时前
python调用mysql
android·python·mysql
萧雾宇5 小时前
Android Compose打造仿现实逼真的烟花特效
android·flutter·kotlin
翻滚丷大头鱼6 小时前
android 性能优化—ANR
android·性能优化
翻滚丷大头鱼6 小时前
android 性能优化—内存泄漏,内存溢出OOM
android·性能优化
拜无忧6 小时前
【教程】flutter常用知识点总结-针对小白
android·flutter·android studio
拜无忧7 小时前
【教程】Flutter 高性能项目架构创建指南:从入门到高性能架构
android·flutter·android studio