我需要从列表中删除一个项目而不改变它。
我可以自己写:
fun <T> Iterable<T>.withoutItemAt(index: Int): List<T> =
take(index) + drop(index + 1)
不过感觉标准库里应该有这个吧。
有这样的功能吗?
最佳答案
你可以使用 filterIndexed
:
fun <T> Iterable<T>.withoutItemAt(index: Int): List<T> =
filterIndexed { i, _ -> i != index }
关于kotlin - 是否有标准的 Kotlin 函数可以返回没有给定索引处的项目的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66171704/