zoukankan      html  css  js  c++  java
  • java集合之ArrayList源码解读

    源自:jdk1.8.0_121
    ArrayList继承自AbstractList,实现了ListRandomAccessCloneableSerializable

    ArrayList内部是通过数组及数组的扩容来实现

    变量

        // 默认容量为10
        private static final int DEFAULT_CAPACITY = 10;
    
        // 空数组
        private static final Object[] EMPTY_ELEMENTDATA = {};
    
        // 默认空数组
        private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    
        // 存放数据的数组,被transient修饰的参数不会被序列化
        transient Object[] elementData;
    
        // 实际元素的大小
        private int size;
    

    构造方法

        public ArrayList(int initialCapacity) {
            if (initialCapacity > 0) {
                this.elementData = new Object[initialCapacity];
            } else if (initialCapacity == 0) {
                this.elementData = EMPTY_ELEMENTDATA;
            } else {
                throw new IllegalArgumentException("Illegal Capacity: "+
                                                   initialCapacity);
            }
        }
    
        public ArrayList() {
            this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
        }
    
        public ArrayList(Collection<? extends E> c) {
            elementData = c.toArray();
            if ((size = elementData.length) != 0) {
                // c.toArray might (incorrectly) not return Object[] (see 6260652)
                if (elementData.getClass() != Object[].class)
                    elementData = Arrays.copyOf(elementData, size, Object[].class);
            } else {
                // replace with empty array.
                this.elementData = EMPTY_ELEMENTDATA;
            }
        }
    

    toArray()实现方式的不同

    注:ArrayList(Collection<? extends E> c) 之所以要判断是否为Object类型,是因为调用toArray()方法的实现方式不同。

    java.util.ArrayList中,toArray()返回的是Object数组。

        public Object[] toArray() {
            return Arrays.copyOf(elementData, size);
        }
    

    java.util.Arrays中,有个名为ArrayList的内部类,当调用Arrays.asList()时,返回的是java.util.Arrays$ArrayList内部类对象,而并非java.util.ArrayListjava.util.Arrays$ArrayList中的toArray()返回的虽然是Object数组,但它是有真是类型的数组。

        private final E[] a;
    
        public Object[] toArray() {
            return a.clone();
        }
    

    ArrayList扩容

        public boolean add(E e) {
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            elementData[size++] = e;
            return true;
        }
    
        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 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);
        }
    

    从源码可以看出,如果1.5倍的elementData.length小于10,会将elementData的大小扩容成默认的10,反之,则会以1.5倍的elementData.length进行扩容。

    将elementData设置为实际容量,动态扩充的多余容量将被删除

        public void trimToSize() {
            modCount++;
            if (size < elementData.length) {
                elementData = (size == 0)
                  ? EMPTY_ELEMENTDATA
                  : Arrays.copyOf(elementData, size);
            }
        }
    
  • 相关阅读:
    搭建strom 的开发环境
    maven 的plugin 的使用
    Maven 的dependency 的 classifier的作用
    Maven中的dependency的scope作用域详解
    Supervisor-进程监控自动重启
    websocket 实战
    vue 监听路由变化
    vux-uploader 图片上传组件
    vue 定义全局函数
    判断对象属性的值是否空,如为空,删除该属性
  • 原文地址:https://www.cnblogs.com/jarjune/p/8341003.html
Copyright © 2011-2022 走看看