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
}

相关方法

相关推荐
秋91 小时前
MySQL 9.7.0 使用详解:新特性、实战与避坑指南
android·数据库·mysql
狼与自由1 小时前
clickhouse ReplacingMergeTree
android·clickhouse
吉吉612 小时前
php反序列化基础知识前奏
android·php·反序列化
努力努力再努力wz2 小时前
【MySQL进阶系列】拒绝冗余SQL:带你透彻理解视图的底层逻辑
android·c语言·数据结构·数据库·c++·sql·mysql
常利兵2 小时前
安卓黑科技:实现多平台商品详情页一键跳转APP
android·科技
_李小白3 小时前
【android opencv学习笔记】Day 5: 高效的图像扫描
android·opencv·学习
liang_jy11 小时前
Android 窗口容器树(一)—— 窗口和窗口容器树
android·源码
HUGu RGIN11 小时前
MySQL--》如何在MySQL中打造高效优化索引
android·mysql·adb
Joseph Cooper13 小时前
Linux/Android 跟踪技术:ftrace、TRACE_EVENT、atrace、systrace 与 perfetto 入门
android·linux·运维
空中海14 小时前
安卓逆向03. 动态调试、抓包分析与 Frida Hook
android