zoukankan      html  css  js  c++  java
  • 【数据结构】ArrayList原理及实现学习总结(2)

    ArrayList是一个基于数组实现的链表(List),这一点可以从源码中看出:

        transient Object[] elementData; // non-private to simplify nested class access

    可以看出ArrayList的内部是给予数组来处理的。

    从ArrayList中查找一个元素的index,其时间复杂度是o(n),其源码如下所示:

    复制代码
        public int indexOf(Object o) {
            if (o == null) {
                for (int i = 0; i < size; i++)
                    if (elementData[i]==null)
                        return i;
            } else {
                for (int i = 0; i < size; i++)
                    if (o.equals(elementData[i]))
                        return i;
            }
            return -1;
        }
    复制代码

    ArrayList支持Clone,是使用Arrays.copyOf(Object[],int)来进行的:

    复制代码
        public Object clone() {
            try {
                ArrayList<?> v = (ArrayList<?>) super.clone();
                v.elementData = Arrays.copyOf(elementData, size);
                v.modCount = 0;
                return v;
            } catch (CloneNotSupportedException e) {
                // this shouldn't happen, since we are Cloneable
                throw new InternalError(e);
            }
        }
    复制代码

    ArrayList中根据index获取数组的时间复杂度是o(1),其源码如下:

    复制代码
        @SuppressWarnings("unchecked")
        E elementData(int index) {
            return (E) elementData[index];
        }
    
        public E get(int index) {//看这里
            rangeCheck(index);
    
            return elementData(index);
        }
    复制代码

    替换指定的位置的元素,时间复杂度也是o(1):

    复制代码
        public E set(int index, E element) {
            rangeCheck(index);
    
            E oldValue = elementData(index);
            elementData[index] = element;
            return oldValue;
        }
    复制代码

    在末尾添加一个元素的时间复杂度也是o(1):

        public boolean add(E e) {
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            elementData[size++] = e;
            return true;
        }

    这里需要注意的是,其容量是可以扩展的,其可以扩展的最大容量是Integer.MAX_VALUE-8,由

    int newCapacity = oldCapacity + (oldCapacity >> 1)

    可以看出,每次是尝试扩容原来的1.5倍:

    复制代码
        private void ensureCapacityInternal(int minCapacity) {
            if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
                minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
            }
    
            ensureExplicitCapacity(minCapacity);
        }
    
        private void ensureExplicitCapacity(int minCapacity) {
            modCount++;
    
            // overflow-conscious code
            if (minCapacity - elementData.length > 0)
                grow(minCapacity);
        }
    
        private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
    
        private void grow(int minCapacity) {
            // overflow-conscious code
            int oldCapacity = elementData.length;
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
            if (newCapacity - MAX_ARRAY_SIZE > 0)
                newCapacity = hugeCapacity(minCapacity);
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
        }
    复制代码

    添加到指定index位置的时间复杂度是o(n),这里需要先把这个位置以及之后的元素统一向后移一位:

    复制代码
        public void add(int index, E element) {
            rangeCheckForAdd(index);
    
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            System.arraycopy(elementData, index, elementData, index + 1,
                             size - index);
            elementData[index] = element;
            size++;
        }
    复制代码

    删除指定index位置的元素时间复杂度也是o(n),这里需要把这个元素之后的所有的元素向前移一位:

    复制代码
        public E remove(int index) {
            rangeCheck(index);
    
            modCount++;
            E oldValue = elementData(index);
    
            int numMoved = size - index - 1;
            if (numMoved > 0)
                System.arraycopy(elementData, index+1, elementData, index,
                                 numMoved);
            elementData[--size] = null; // clear to let GC do its work
    
            return oldValue;
        }
    复制代码

    删除一个元素的时间复杂度也是o(n),显示查出来这个元素,删除,之后是把后面的元素向前进一位:

    复制代码
        public boolean remove(Object o) {
            if (o == null) {
                for (int index = 0; index < size; index++)
                    if (elementData[index] == null) {
                        fastRemove(index);
                        return true;
                    }
            } else {
                for (int index = 0; index < size; index++)
                    if (o.equals(elementData[index])) {
                        fastRemove(index);
                        return true;
                    }
            }
            return false;
        }
    
        private void fastRemove(int index) {
            modCount++;
            int numMoved = size - index - 1;
            if (numMoved > 0)
                System.arraycopy(elementData, index+1, elementData, index,
                                 numMoved);
            elementData[--size] = null; // clear to let GC do its work
        }
    复制代码

    虽然在申明存储数组的时候,申明了不可被序列化,但是只要保存的对象是可序列化的,这个ArrayList还是可以序列化的:

    复制代码
        private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException{
            // Write out element count, and any hidden stuff
            int expectedModCount = modCount;
            s.defaultWriteObject();
    
            // Write out size as capacity for behavioural compatibility with clone()
            s.writeInt(size);
    
            // Write out all elements in the proper order.
            for (int i=0; i<size; i++) {
                s.writeObject(elementData[i]);
            }
    
            if (modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
        }
    
        private void readObject(java.io.ObjectInputStream s)
            throws java.io.IOException, ClassNotFoundException {
            elementData = EMPTY_ELEMENTDATA;
    
            // Read in size, and any hidden stuff
            s.defaultReadObject();
    
            // Read in capacity
            s.readInt(); // ignored
    
            if (size > 0) {
                // be like clone(), allocate array based upon size not capacity
                ensureCapacityInternal(size);
    
                Object[] a = elementData;
                // Read in all elements in the proper order.
                for (int i=0; i<size; i++) {
                    a[i] = s.readObject();
                }
            }
        }
    复制代码

     从以上的情况来看,ArrayList不是线程安全的,在进行index查找和最后插入的时候具有比较明显的时间复杂度优势。

    但是,ArrayList的扩容操作,以及扩容产生的空间浪费一直是被人诟病的地方,另外在其中间进行插入的操作也不尽人意,时间复杂度是o(n)。

  • 相关阅读:
    跨域上传图片的尝试过程,最终成功了。哈哈
    老子再也不加什么所谓的技术群了,顶撞群主的话,就被踢了。
    开发使用的插件
    设计原则记录
    程序员修神之路--redis做分布式锁可能不那么简单
    程序员过关斩将--面试官再问你Http请求过程,怼回去!
    程序员修神之路--问世间异步为何物?
    程序员修神之路--提高网站的吞吐量
    程序员过关斩将--你的业务是可变的吗(福利你领了吗)
    程序员修神之路--🤠分布式高并发下Actor模型如此优秀🤠
  • 原文地址:https://www.cnblogs.com/gxz-sw/p/8601857.html
Copyright © 2011-2022 走看看