gpt4 book ai didi

android - 1.3 中缺少 Kotlin 的 ArrayList.removeRange 选项?

转载 作者:行者123 更新时间:2023-12-02 12:41:30 26 4
gpt4 key购买 nike

Kotlin 有没有像 Java 一样删除特定范围内元素的函数。

在 Java 中,我们有一个名为 removeRange 的方法,它可以被扩展并变得有用。

expect class ArrayList<E> :MutableList<E>,RandomAccess{
constructor()
constructor(initialCapacity:Int)
constructor(elements:Collection<E>)

fun trimToSize()
fun ensureCapacity(minCapacity:Int)

// From List

override val size:Int
override fun isEmpty():Boolean
override fun contains(element:@UnsafeVariance E):Boolean
override fun containsAll(elements:Collection<@UnsafeVariance E>):Boolean
override operator fun get(index:Int):E
override fun indexOf(element:@UnsafeVariance E):Int
override fun lastIndexOf(element:@UnsafeVariance E):Int

// From MutableCollection

override fun iterator():MutableIterator<E>

// From MutableList

override fun add(element:E):Boolean
override fun remove(element:E):Boolean
override fun addAll(elements:Collection<E>):Boolean
override fun addAll(index:Int,elements:Collection<E>):Boolean
override fun removeAll(elements:Collection<E>):Boolean
override fun retainAll(elements:Collection<E>):Boolean
override fun clear()
override operator fun set(index:Int,element:E):E
override fun add(index:Int,element:E)
override fun removeAt(index:Int):E
override fun listIterator():MutableListIterator<E>
override fun listIterator(index:Int):MutableListIterator<E>
override fun subList(fromIndex:Int,toIndex:Int):MutableList<E>
}

removeRange was added in 1.1 of Kotlin but was removed in 1.3

最佳答案

removeRange 在 kotlin 中受到保护,但这应该可以解决问题:

array.subList(2, 4).clear();

关于android - 1.3 中缺少 Kotlin 的 ArrayList.removeRange 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56902691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com