標籤:strong spec inter lis 先來 obj overflow for迴圈 就是
Java集合之ArrayList - 吃透增刪查改
從源碼看初始化以及增刪查改,學習ArrayList。
先來看下ArrayList定義的幾個屬性:
private static final int DEFAULT_CAPACITY = 10;private static final Object[] EMPTY_ELEMENTDATA = {};private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};transient Object[] elementData; // 儲存對象private int size; // ArrayList的長度
從這裡可以看到ArrayList內部使用數組實現的。
一. 初始化
1. ArrayList()
無參的構造器代:
/** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
可以看到這個構造器初始化了一個空數組。這裡有個疑問,就是注釋明明說是構造了一個長度為10的數組,其實這是在添加第一個元素的時候才進行的擴充。
2. ArrayList(int initialCapacity)
指定長度的構造器:
/** * Constructs an empty list with the specified initial capacity. * * @param initialCapacity the initial capacity of the list * @throws IllegalArgumentException if the specified initial capacity * is negative */ 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); } }
這個構造器顯式的指明了數組的長度,其實如果小於10的話,在添加第一個元素的時候還是會擴充到長度為10的數組。
二. 增加元素
1. add(E e)
/** * 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}) * 添加指定的元素到List的末尾 */ public boolean add(E e) { ensureCapacityInternal(size + 1); // 擴充數組長度 elementData[size++] = e; // 最後一個賦值為e return true; }
那麼它是如何擴充數組長度的呢?追蹤代碼來到grow(int minCapacity)方法:
/** 可以看到它是通過Arrays.copyOf方法將原數組拷貝到了一個數組長度為newCapacity的新數組裡面*/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); }
2. add(int index, E element)
/** * 在指定的位置添加一個元素 */ public void add(int index, E element) { rangeCheckForAdd(index); // 檢查index是否合法 ensureCapacityInternal(size + 1); // 擴充數組長度 System.arraycopy(elementData, index, elementData, index + 1, size - index); // 通過拷貝返回一個新數組 elementData[index] = element; size++; }
這裡用到了System類的arraycopy方法,它的用法如下:
System. arraycopy(Object src, int srcPos, Object dest, int destPos,int length)
src:原數組;
srcPos:源數組要複製的起始位置;
dest:目標數組;
destPos:目標數組放置的起始位置;
length:複製的長度
這個方法也可以實現自身的複製。上面的就是利用了自身賦值的特性進行數組的拷貝。相當於將index後面的所有元素後移了一位,將新元素插入到index位置。
三. 刪除元素
1. remove(int index)
/* * 和add(int index, E element)類似,使用System.arraycopy進行自身拷貝,相當於將index後面的元素 * 像前移動了一位,覆蓋掉需要刪除的元素,將最後一個元素置位null,等待JVM自動回收 */ 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; }
2. remove(Object o)
/* * 先通過for迴圈找到o所在的位置,再通過fastRemove(index)移除,實現方法和remove(int index)一樣 */ 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; }
四. 尋找元素
/* * 尋找元素就比較簡單了,直接通過數組的下角標進行返回 */ public E get(int index) { rangeCheck(index); return elementData(index); }
五. 修改元素
/* * 修改元素也比較簡單,直接通過數組的下角標進行賦值 */ public E set(int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; }
總結
從源碼可以看到,ArrayList以數組方式實現,尋找和修改的效能比較好,增加和刪除的效能就比較差了。
從源碼看Java集合之ArrayList