Kotlin中定义List变量却发现找不到clear、addAll等常用方法

问题描述:

 private val mDatas: List<String> = ArrayList()

上面这个kotlin代码中定义了一个List,但是在使用的时候却发现没有clear、addAll等常用方法

分析问题:

分析kotlin源码发现,List接口中确实没有定义clear、addAll等常用方法方法

/**
 * A generic ordered collection of elements. Methods in this interface support only read-only access to the list;
 * read/write access is supported through the [MutableList] interface.
 * @param E the type of elements contained in the list. The list is covariant on its element type.
 */
public interface List<out E> : Collection<E> {
    // Query Operations
    override val size: Int

    override fun isEmpty(): Boolean
    override fun contains(element: @UnsafeVariance E): Boolean
    override fun iterator(): Iterator<E>

    // Bulk Operations
    override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean

    // Positional Access Operations
    /**
     * Returns the element at the specified index in the list.
     */
    public operator fun get(index: Int): E

    // Search Operations
    /**
     * Returns the index of the first occurrence of the specified element in the list, or -1 if the specified
     * element is not contained in the list.
     */
    public fun indexOf(element: @UnsafeVariance E): Int

    /**
     * Returns the index of the last occurrence of the specified element in the list, or -1 if the specified
     * element is not contained in the list.
     */
    public fun lastIndexOf(element: @UnsafeVariance E): Int

    // List Iterators
    /**
     * Returns a list iterator over the elements in this list (in proper sequence).
     */
    public fun listIterator(): ListIterator<E>

    /**
     * Returns a list iterator over the elements in this list (in proper sequence), starting at the specified [index].
     */
    public fun listIterator(index: Int): ListIterator<E>

    // View
    /**
     * Returns a view of the portion of this list between the specified [fromIndex] (inclusive) and [toIndex] (exclusive).
     * The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
     *
     * Structural changes in the base list make the behavior of the view undefined.
     */
    public fun subList(fromIndex: Int, toIndex: Int): List<E>
}

而ArrayList继承的是MutableList:

package kotlin.collections

public final expect class ArrayList<E> : kotlin.collections.MutableList<E>, kotlin.collections.RandomAccess {
    public constructor() { /* compiled code */ }

    public constructor(initialCapacity: kotlin.Int) { /* compiled code */ }

    public constructor(elements: kotlin.collections.Collection<E>) { /* compiled code */ }

    public expect open val size: kotlin.Int /* compiled code */

    public open expect fun add(element: E): kotlin.Boolean { /* compiled code */ }

    public open expect fun add(index: kotlin.Int, element: E): kotlin.Unit { /* compiled code */ }

    public open expect fun addAll(index: kotlin.Int, elements: kotlin.collections.Collection<E>): kotlin.Boolean { /* compiled code */ }

    public open expect fun addAll(elements: kotlin.collections.Collection<E>): kotlin.Boolean { /* compiled code */ }

    public open expect fun clear(): kotlin.Unit { /* compiled code */ }

    public open expect operator fun contains(element: E): kotlin.Boolean { /* compiled code */ }

    public open expect fun containsAll(elements: kotlin.collections.Collection<E>): kotlin.Boolean { /* compiled code */ }

    public final expect fun ensureCapacity(minCapacity: kotlin.Int): kotlin.Unit { /* compiled code */ }

    public open expect operator fun get(index: kotlin.Int): E { /* compiled code */ }

    public open expect fun indexOf(element: E): kotlin.Int { /* compiled code */ }

    public open expect fun isEmpty(): kotlin.Boolean { /* compiled code */ }

    public open expect operator fun iterator(): kotlin.collections.MutableIterator<E> { /* compiled code */ }

    public open expect fun lastIndexOf(element: E): kotlin.Int { /* compiled code */ }

    public open expect fun listIterator(): kotlin.collections.MutableListIterator<E> { /* compiled code */ }

    public open expect fun listIterator(index: kotlin.Int): kotlin.collections.MutableListIterator<E> { /* compiled code */ }

    public open expect fun remove(element: E): kotlin.Boolean { /* compiled code */ }

    public open expect fun removeAll(elements: kotlin.collections.Collection<E>): kotlin.Boolean { /* compiled code */ }

    public open expect fun removeAt(index: kotlin.Int): E { /* compiled code */ }

    public open expect fun retainAll(elements: kotlin.collections.Collection<E>): kotlin.Boolean { /* compiled code */ }

    public open expect operator fun set(index: kotlin.Int, element: E): E { /* compiled code */ }

    public open expect fun subList(fromIndex: kotlin.Int, toIndex: kotlin.Int): kotlin.collections.MutableList<E> { /* compiled code */ }

    public final expect fun trimToSize(): kotlin.Unit { /* compiled code */ }
}

再看看MutableList源码可以发现,常用的clear、addAll等方法都有定义:

/**
 * A generic ordered collection of elements that supports adding and removing elements.
 * @param E the type of elements contained in the list. The mutable list is invariant on its element type.
 */
public interface MutableList<E> : List<E>, MutableCollection<E> {
    // Modification Operations
    override fun add(element: E): Boolean

    override fun remove(element: E): Boolean

    // Bulk Modification Operations
    override fun addAll(elements: Collection<E>): Boolean

    /**
     * Inserts all of the elements in the specified collection [elements] into this list at the specified [index].
     *
     * @return `true` if the list was changed as the result of the operation.
     */
    public 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(): Unit

    // Positional Access Operations
    /**
     * Replaces the element at the specified position in this list with the specified element.
     *
     * @return the element previously at the specified position.
     */
    public operator fun set(index: Int, element: E): E

    /**
     * Inserts an element into the list at the specified [index].
     */
    public fun add(index: Int, element: E): Unit

    /**
     * Removes an element at the specified [index] from the list.
     *
     * @return the element that has been removed.
     */
    public fun removeAt(index: Int): E

    // List Iterators
    override fun listIterator(): MutableListIterator<E>

    override fun listIterator(index: Int): MutableListIterator<E>

    // View
    override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>
}

解决问题:

所以解决的办法就是将变量类型使用"MutableList"代替"List"就可以了,完整的定义如下代码:

private val mDatas: MutableList<String> = ArrayList()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值