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

相关用法

相关推荐
百锦再2 小时前
React编程高级主题:测试代码
android·前端·javascript·react.js·前端框架·reactjs
2501_916008893 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
玉梅小洋4 小时前
Windows 10 Android 构建配置指南
android·windows
Libraeking6 小时前
视觉篇:Canvas 自定义绘图与高级动画的华丽圆舞曲
android·经验分享·android jetpack
Fushize6 小时前
多模块架构下的依赖治理:如何避免 Gradle 依赖地狱
android·架构·kotlin
Jomurphys7 小时前
Kotlin - 类型别名 typealias
android·kotlin
Haha_bj7 小时前
Flutter ——flutter_screenutil 屏幕适配
android·ios
zh_xuan7 小时前
kotlin lazy委托异常时执行流程
开发语言·kotlin
Haha_bj7 小时前
Flutter ——device_info_plus详解
android·flutter·ios
前端小伙计7 小时前
Android/Flutter 项目统一构建配置最佳实践
android·flutter