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]

相关用法

相关推荐
Android系统攻城狮7 分钟前
Android16之交叉编译系统压力测试利器:stress-ng(二百六十六)
android·压力测试·android16·系统调试
杨忆8 分钟前
导航栏左右拖动切换
android
shuaijie051814 分钟前
在Vue.js中实现列表的拖动功能,使用第三方库如vuedraggable(基于Sortable.js)
android·javascript·vue.js
毕设源码-郭学长18 分钟前
【开题答辩全过程】以 基于Android的儿童托管系统为例,包含答辩的问题和答案
android
sky丶Mamba23 分钟前
CentOS Stream 9安装MySQL
android·mysql·centos
wei1155634 分钟前
framework.jar使用
android
Lei活在当下9 小时前
【项目踩坑实录】并发环境下,Glide缓存引起的图片加载异常
android·debug·glide
my_power52011 小时前
检出git项目到android studio该如何配置
android·git·android studio
三少爷的鞋14 小时前
Repository 方法设计:suspend 与 Flow 的决选择指南(以朋友圈为例)
android
阿里云云原生15 小时前
Android App 崩溃排查指南:阿里云 RUM 如何让你快速从告警到定位根因?
android·java