ArrayList結(jié)構(gòu)圖

arraylist.png
ArrayList主要方法
- public boolean add(E e);
- public E set(int index, E element)
- public boolean remove(Object o);
- public E get(int index);
ArrayList解讀主要方法
首先來(lái)看看add方法源碼:
public boolean add(E e) {
//判斷是否需要擴(kuò)容
ensureCapacityInternal(size + 1); // Increments modCount!!
// 添加元素
elementData[size++] = e;
return true;
}
來(lái)看一下ensureCapacityInternal這個(gè)方法:
private void ensureCapacityInternal(int minCapacity) {
// 如果數(shù)據(jù)為空(集合里沒(méi)有元素),minCapacity取默認(rèn)長(zhǎng)度
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
來(lái)看一下ensureExplicitCapacity這個(gè)方法:
private void ensureExplicitCapacity(int minCapacity) {
// 這個(gè)值是為了判斷ConcurrentModificationException異常的
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
來(lái)看一下grow這個(gè)方法:
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//右移一位擴(kuò)容
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:
//擴(kuò)充elementData容量
elementData = Arrays.copyOf(elementData, newCapacity);
}
在看一下底層的調(diào)用:
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
來(lái)看一下public E set(int index, E element)源碼
public E set(int index, E element) {
//判斷index位置
rangeCheck(index);
//獲取index位置的元素
E oldValue = elementData(index);
//把新的元素放入index位置,返回原來(lái)的元素
elementData[index] = element;
return oldValue;
}
//判斷index位置
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
再看看remove的源碼:
public E remove(int index) {
//校驗(yàn)刪除位置是否合理
rangeCheck(index);
// 同add理由一樣
modCount++;
// 保存待刪除元素
E oldValue = elementData(index);
// 判斷刪除位置:如果>0不在尾部需要調(diào)用System.arraycopy,否則在尾部直接刪除
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
//返回當(dāng)前要?jiǎng)h除的元素
return oldValue;
}
最后看一下get方法:
public E get(int index) {
// 校驗(yàn)刪除位置是否合理
rangeCheck(index);
//返回index位置的元素
return elementData(index);
}
好了看了一下,也就是add方法比較復(fù)雜,remove和get都比較簡(jiǎn)單。
ArrayList遍歷介紹
常用的三種遍歷方式:
//one foreach 遍歷
for (Object o : list) {
System.out.println(o);
}
// two 隨機(jī)訪問(wèn)
for (int i = 0; i <list.size(); i++) {
System.out.println(list.get(i));
}
//three 迭代器的遍歷
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
ArrayList其他特性介紹
我們來(lái)介紹一下ArrayList的一些小特效:
- ArrayList底層是利用數(shù)組來(lái)實(shí)現(xiàn)的 Object[] elementData,因此ArrayList擁有數(shù)組的特性,因?yàn)閿?shù)組是邏輯上連續(xù)的,物理上也是連續(xù)的,因此查詢比較方便,刪除比較麻煩;然而LinkedList底層是利用鏈表來(lái)實(shí)現(xiàn)的,它擁有鏈表的特性,因?yàn)殒湵碇皇沁壿嬌线B續(xù)的物理上非連續(xù)的所以它的添加和刪除比較快,查詢就比較麻煩。
- ArrayList默認(rèn)的初始值是10,private static final int DEFAULT_CAPACITY = 10;每次擴(kuò)容的容量為: int newCapacity = oldCapacity + (oldCapacity >> 1);約1.5倍
- ArrayList是會(huì)拋出 ConcurrentModificationException異常的