Kotlin drop用法及代码示例

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

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

用法

kotlin 复制代码
fun <T> Array<out T>.drop(n: Int): List<T>

fun ByteArray.drop(n: Int): List<Byte>

fun ShortArray.drop(n: Int): List<Short>

fun IntArray.drop(n: Int): List<Int>

fun LongArray.drop(n: Int): List<Long>

fun FloatArray.drop(n: Int): List<Float>

fun DoubleArray.drop(n: Int): List<Double>

fun BooleanArray.drop(n: Int): List<Boolean>

fun CharArray.drop(n: Int): List<Char>

fun <T> Iterable<T>.drop(n: Int): List<T>

@ExperimentalUnsignedTypes fun UIntArray.drop(
    n: Int
): List<UInt>

@ExperimentalUnsignedTypes fun ULongArray.drop(
    n: Int
): List<ULong>

@ExperimentalUnsignedTypes fun UByteArray.drop(
    n: Int
): List<UByte>

@ExperimentalUnsignedTypes fun UShortArray.drop(
    n: Int
): List<UShort>

返回包含除第一个 n 元素之外的所有元素的列表。

代码示例

kotlin 复制代码
import kotlin.test.* 

fun main(args: Array<String>) { 
    //sampleStart 
    val chars = ('a'..'z').toList() 
    println(chars.drop(23)) // [x, y, z] 
    println(chars.dropLast(23)) // [a, b, c] 
    println(chars.dropWhile { it < 'x' }) // [x, y, z] 
    println(chars.dropLastWhile { it > 'c' }) // [a, b, c] 
    //sampleEnd 
}

// 输出
[x, y, z]
[a, b, c]
[x, y, z]
[a, b, c]

异常IllegalArgumentException- 如果n是负数。

相关用法

相关推荐
砖厂小工3 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心3 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心3 小时前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker6 小时前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴6 小时前
Android17 为什么重写 MessageQueue
android
冬奇Lab18 小时前
OpenClaw 源码精读(2):Channel & Routing——一条消息如何找到它的 Agent?
人工智能·开源·源码阅读
桦说编程19 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android