ArrayList源碼分析

ArrayList是一種最常用的集合類,底層數據結構是數組,提供動態擴展數組長度的特性,允許元素的值爲null。ArrayList是一種非線程安全的集合類,若要在多線程的環境,需要注意同步問題,也可以使用Collections.synchronizedList()方法保證線程安全問題。

繼承關係

ArrayList

構造方法

默認構造方法,創建一個空的數組對象

    public ArrayList() {
        super();
        this.elementData = EMPTY_ELEMENTDATA;
    }
    private static final Object[] EMPTY_ELEMENTDATA = {};

創建指定大小的數組對象

    public ArrayList(int initialCapacity) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "  
                    initialCapacity);
        this.elementData = new Object[initialCapacity];
    }

根據一個集合對象創建ArrayList

    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        size = elementData.length;
        // c.toArray()的返回結果可能並不是Object數組對象
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    }

主要方法

add

    public boolean add(E e) {
        ensureCapacityInternal(size   1);
        elementData[size  ] = e;
        return true;
    }
    // 確定數組大小
    private void ensureCapacityInternal(int minCapacity) {
        if (elementData == EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }
    // 數組擴容
    private void ensureExplicitCapacity(int minCapacity) {
        // 記錄ArrayList結構被修改的次數
        modCount  ;

        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
        // 當前數組大小的1.5倍
        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);
    }

    
   public void add(int index, E element) {
        // 數組越界檢測    
       rangeCheckForAdd(index);

       ensureCapacityInternal(size   1);
        // 將元素插在指定的位置
       System.arraycopy(elementData, index, elementData, index   1,
               size - index);
       elementData[index] = element;
       size  ;
   }

get

返回指定位置的元素

    public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }

set

將原數組中的元素返回,並將新元素插入

    public E set(int index, E element) {
        rangeCheck(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }

remove

    // 移除指定位置元素
    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;
    }
    
    // 移除指定對象
    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;
    }

本文由博客一文多發平臺 OpenWrite 發佈!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章