JDK8之ArrayList源碼

 

ArrayList三個構造器

 

/**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10; //初始化容量大小 /**
     * Shared empty array instance used for empty instances.
     */
    private static final Object[] EMPTY_ELEMENTDATA = {};

    /**
     * Shared empty array instance used for default sized empty instances. We
     * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
     * first element is added.
     */
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

    /**
     * The array buffer into which the elements of the ArrayList are stored.
     * The capacity of the ArrayList is the length of this array buffer. Any
     * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
     * will be expanded to DEFAULT_CAPACITY when the first element is added.
     */
    transient Object[] elementData; // non-private to simplify nested class access //保存數據的數組

    /**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
    private int size;//ArrayList實際數據的數量

三個構造器主要目的是初始化Object[] 的長度數組

一、ArrayList(),默認構造數組長度爲10空列表,可是DEFAULTCAPACITY_EMPTY_ELEMENTDATA長度咱們並不知道,真正設置數組長度是在add()方法擴容時候設置app

    /**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

 二、new Array(),指定數組長度的列表,這個構造器的優勢是若是咱們知道數據的大小,初始化時指定長度則會減小數組擴容的次數,大大提升ArrayList的效率ui

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);
        }
    }

 

三、new ArrayList(Collection c),這個構造器是能夠把其它實現Collection接口的類當作參數,構造Object[] this

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;
        }
    }

ArrayList-add

一、add(E e) spa

/**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     */
    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);//DEFAULT_CAPACITY默認是10,Math.max 取最大值
}
ensureExplicitCapacity(minCapacity);
}

 

 

 private void ensureExplicitCapacity(int minCapacity) {
        modCount++;//modCount 數組修改次數 ,不管add remove 都會++
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
相關文章
相關標籤/搜索