Kotlin dropLastWhile用法及代码示例

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

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

用法

kotlin 复制代码
inline fun <T> Array<out T>.dropLastWhile(
    predicate: (T) -> Boolean
): List<T>

inline fun ByteArray.dropLastWhile(
    predicate: (Byte) -> Boolean
): List<Byte>

inline fun ShortArray.dropLastWhile(
    predicate: (Short) -> Boolean
): List<Short>

inline fun IntArray.dropLastWhile(
    predicate: (Int) -> Boolean
): List<Int>

inline fun LongArray.dropLastWhile(
    predicate: (Long) -> Boolean
): List<Long>

inline fun FloatArray.dropLastWhile(
    predicate: (Float) -> Boolean
): List<Float>

inline fun DoubleArray.dropLastWhile(
    predicate: (Double) -> Boolean
): List<Double>

inline fun BooleanArray.dropLastWhile(
    predicate: (Boolean) -> Boolean
): List<Boolean>

inline fun CharArray.dropLastWhile(
    predicate: (Char) -> Boolean
): List<Char>

inline fun <T> List<T>.dropLastWhile(
    predicate: (T) -> Boolean
): List<T>

@ExperimentalUnsignedTypes inline fun UIntArray.dropLastWhile(
    predicate: (UInt) -> Boolean
): List<UInt>

@ExperimentalUnsignedTypes inline fun ULongArray.dropLastWhile(
    predicate: (ULong) -> Boolean
): List<ULong>

@ExperimentalUnsignedTypes inline fun UByteArray.dropLastWhile(
    predicate: (UByte) -> Boolean
): List<UByte>

@ExperimentalUnsignedTypes inline fun UShortArray.dropLastWhile(
    predicate: (UShort) -> Boolean
): List<UShort>

返回一个列表,其中包含除满足给定 predicate 的最后一个元素之外的所有元素。

代码例子

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]

相关用法

相关推荐
00后程序员张19 分钟前
iOS 26 App 运行状况全面解析 多工具协同监控与调试实战指南
android·ios·小程序·https·uni-app·iphone·webview
2501_916007472 小时前
iOS 混淆实战,多工具组合完成 IPA 混淆、加固与发布治理(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 小时前
怎么上架 App?iOS 应用上架完整流程详解与跨平台发布实战指南
android·ios·小程序·https·uni-app·iphone·webview
2501_929157682 小时前
【安卓+PC+IOS】psp全中文游戏+高清纹理包+金手指
android·游戏·ios
2501_916008892 小时前
iOS 混淆工具链实战 多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码加固|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
yinghuaqipao2 小时前
面向对象——设计模式(创建型)
android·java·设计模式
用户41659673693552 小时前
Android 性能调优与故障排查:ADB 诊断命令终极指南
android
沐怡旸2 小时前
【底层机制】【Android】本地Socket 对比 Binder 以及在 Android系统中的应用
android·面试
w_y_fan2 小时前
flutter_native_splash: ^2.4.7
android·前端·flutter
沐怡旸3 小时前
【底层机制】【Android】Binder 驱动作用
android·面试