美文网首页
Kotlin中Arraylist无法使用clear(),addA

Kotlin中Arraylist无法使用clear(),addA

作者: IT键盘侠 | 来源:发表于2018-11-30 14:24 被阅读0次
    • 在改写一个方法过程中,发现好端端的Arraylist无法清除与addAll()
    image.png
    • 查看mList定义,习惯了java中的写法
    // 数据list
        protected var mList: List<M> = ArrayList()
    
    • 查看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>
    }
    
    • 注意到kotlin中ArrayList继承自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>
    }
    

    -故问题得到解决,重新定义mList变量,问题得到解决。完~

    // 数据list
        protected var mList: MutableList<M> = ArrayList()
    
    

    相关文章

      网友评论

          本文标题:Kotlin中Arraylist无法使用clear(),addA

          本文链接:https://www.haomeiwen.com/subject/wvufcqtx.html