ArrayList源碼學習

容器初始化方法:

兩種初始化方法

  1. 不指定初始容器大小
ArrayList<String> arrayList = new ArrayList<String>();

內部源碼操作

public ArrayList() {
       super();
       this.elementData = EMPTY_ELEMENTDATA;
   }
EMPTY_ELEMENTDATA 是一個空的Object類型的數組
private static final Object[] EMPTY_ELEMENTDATA = {};
  1. 指定初始化容器大小
ArrayList<String> arrayList = new ArrayList<String>(2);
//如果是指定初始化大小,內部會把this.elementData = new Object[initialCapacity];這個參數初始化一個大小

添加方法

有4種添加方法:add(E e),add(int index,E e),addAll(Collection c),addAll(int index , Collection c);
add(E e)方法源碼:

public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // 保證容器長度
        elementData[size++] = e; //把添加的元素放到最后,這個size會記錄當前添加的時第幾個,然后下一個是在哪個位置
        return true;
    }

 private void ensureCapacityInternal(int minCapacity) {
        if (elementData == EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);//如果容器沒有指定初始化大小,則初始化一個最小的,jdk1.7里面指定的最小的大小是10
        }

        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);  //內部使用了System.arraycopy復制數組
    }


add(int index ,E e )源碼:

public void add(int index, E element) {
        rangeCheckForAdd(index);//檢查是否越界

        ensureCapacityInternal(size + 1);  // 先擴容,保證后面復制的時候需要的長度
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index); //主要是這里調用了api復制數組,移動index位置到后面一個位置,留出位置放
        elementData[index] = element;放數據
        size++;//保證記錄的size是下次需要記錄的最后的位置
    }

addAll(Collection c)源碼:

public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray(); 
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }
跟add(int index,E e)差不多

addAll(int index, Collection<? extends E> c)源碼:

 public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount

        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew,
                             numMoved);

        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }

刪除方法

remove(int index),remove(Object obj),removeAll(Collection c)
reomve(Object obj)方法源碼:

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; // 差不多,也是使用arrayCooy方法   根據下標移除也差不多,removeAll也差不多
    }

獲取方法

get(int index):直接數組下標取值

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容