【问题标题】:ArrayList.removeRange option for Kotlin is missing in 1.3?1.3 中缺少 Kotlin 的 ArrayList.removeRange 选项?
【发布时间】:2026-02-20 22:30:02
【问题描述】:

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 在 Kotlin 1.1 中被添加,但在 1.3 中被移除

【问题讨论】:

  • 你检查过这个removeRange
  • 我在使用 arraylist @NileshRathod 时找不到这样的函数
  • 你能给我看看它的使用例子吗
  • 您可以为此创建扩展功能

标签: android arraylist kotlin kotlin-android-extensions


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    没有干净的方法可以做到这一点。 Shermano 是对的。这不是很好,但这是最好的方法。您的选择

    a) 使用dropLastdrop

    但是,您不能对 ArrayList 使用类型转换

    lis = lis.dropLast(1) as ArrayList<Int>  
    

    它给出了运行时错误。必须声明像MutableList 这样的变量,这几乎是一样的。

       var lis = mutableListOf(2,3,4)
       lis = lis.dropLast(2) as MutableList<Int>
       println("dropLast: ${lis.size} ${lis[0]}")
    

    打印出来

    dropLast: 1 2
    

    您也可以使用lis.dropLast(2).toMutableList()

    b)使用for声明

    var lis=arrayListOf(2,3,4)   
    for (i in lis.indices.reversed()) {
       if (i>=1 && i<=2) lis.removeAt(i)
    }
    println("for: ${lis.size} ${lis[0]}")
    

    注意需要向后删除。

    打印出来

    for: 1 2
    

    c) 使用subList

       var lis = arrayListOf(2,3,4)
       lis.subList(1,3).clear()
       println("subList: ${lis.size} ${lis[0]}")
    

    打印出来

    subList: 1 2
    

    /*/

    我更喜欢将第三个选项封装在一些内联函数中。

    inline fun <T> ArrayList<T>.delLen(ini:Int, len:Int=-1) {
      if (len == -1)
        this.subList(ini,this.size).clear()
      else
        this.subList(ini,ini+len).clear()
    }
    

    所以

    lis.delLen(0,2) // delete the 0th postion for 2 positions.
    lis.dellen(1)  // delete from 1st postion forward. 
    

    我也喜欢

    inline fun <T> ArrayList<T>.delLast(n:Int=1) {
      this.subList(this.size-n,this.size).clear()
    }
    

    例子

    lis.delLast() // delete the last position
    list.delLast(3) // delete last 3 positions
    

    【讨论】: