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是负数。

相关用法

相关推荐
Lei活在当下1 天前
【Perfetto从入门到精通】4.使用 heapprofd 工具采样追踪 Java/Native 内存分配
android·性能优化·架构
alexhilton1 天前
学会在Jetpack Compose中加载Lottie动画资源
android·kotlin·android jetpack
summerkissyou19871 天前
Android-Camera-为啥不移到packages/module
android·相机
liang_jy1 天前
Android UID
android·面试
nono牛1 天前
安卓/MTK平台日志关键词详解
android
TimeFine1 天前
Android AI解放生产力(四)实战:解放绘制UI的繁琐工作
android
sheji34161 天前
【开题答辩全过程】以 基于Android的社区车位共享管理系统的设计与实现为例,包含答辩的问题和答案
android
TimeFine1 天前
Android AI解放生产力(三):认识custom_prompts和skills
android
summerkissyou19871 天前
Android-Audio-为啥不移到packages/module
android·音视频
catchadmin1 天前
PHP 值对象实战指南:避免原始类型偏执
android·开发语言·php