首先
public class ArrayList<E> extends AbstractList<E> implements Cloneable, Serializable, RandomAccess
ArrayList類實現(xiàn)了這三個接口, 具有可復制的弧蝇,可序列化的延赌,以及快速隨機訪問的特性悯舟。
一愕贡, 構(gòu)造函數(shù)
public ArrayList(int capacity) { // EmptyArray.OBJECT 其實就是new Object[0])潦嘶;
if (capacity < 0) {
throw new IllegalArgumentException("capacity < 0: " + capacity);
}
array = (capacity == 0 ? EmptyArray.OBJECT : new Object[capacity]);
}
public ArrayList() {
array = EmptyArray.OBJECT;
}
public ArrayList(Collection<? extends E> collection) {
if (collection == null) {
throw new NullPointerException("collection == null");
}
Object[] a = collection.toArray();
if (a.getClass() != Object[].class) {
Object[] newArray = new Object[a.length];
System.arraycopy(a, 0, newArray, 0, a.length);
a = newArray;
}
array = a;
size = a.length;
}
add()和addAll()方法 创南。
1.添加add(E object)
@Override public boolean add(E object) {
Object[] a = array;
int s = size;
if (s == a.length) { //數(shù)組里剛好放滿伦忠。(有可能 不相等, 有剩余容量 稿辙。a.length 有可能 大于size.)
Object[] newArray = new Object[s + //MIN_CAPACITY_INCREMENT = 12;
(s < (MIN_CAPACITY_INCREMENT / 2) ? // 如果原數(shù)組個數(shù)少于6個,擴容增加12個缓苛, 否則,容量增加當前容量的一半。
MIN_CAPACITY_INCREMENT : s >> 1)];
System.arraycopy(a, 0, newArray, 0, s);
array = a = newArray; // 替換為新數(shù)組未桥。
}
a[s] = object; // 最后設(shè)置為剛添加的數(shù)
size = s + 1;
modCount++;
return true;
}
2 .add(int index, E object) 指定添加位置
@Override public void add(int index, E object) {
Object[] a = array;
int s = size;
if (index > s || index < 0) {
throwIndexOutOfBoundsException(index, s);
}
if (s < a.length) { // 數(shù)組中有末用空間笔刹,直接添加這個數(shù)
System.arraycopy(a, index, a, index + 1, s - index);//數(shù)組在指定位置 全部后移,
} else {
// assert s == a.length;
Object[] newArray = new Object[newCapacity(s)]; // newCapacity 擴容冬耿。
System.arraycopy(a, 0, newArray, 0, index); //拷貝到新數(shù)據(jù)newArray里
System.arraycopy(a, index, newArray, index + 1, s - index); // /數(shù)組在指定位置 全部后移舌菜,
array = a = newArray; // 設(shè)置為當前數(shù)組
}
a[index] = object; // 在指定位置插入值
size = s + 1;
modCount++;
}
3,addAll(Collection<? extends E> collection)添加集合
@Override public boolean addAll(Collection<? extends E> collection) {
Object[] newPart = collection.toArray();
int newPartSize = newPart.length;
if (newPartSize == 0) {
return false;
}
Object[] a = array;
int s = size;
int newSize = s + newPartSize; // If add overflows, arraycopy will fail
if (newSize > a.length) {
int newCapacity = newCapacity(newSize - 1); // ~33% growth room
Object[] newArray = new Object[newCapacity];
System.arraycopy(a, 0, newArray, 0, s);
array = a = newArray;
}
System.arraycopy(newPart, 0, a, s, newPartSize);
size = newSize;
modCount++;
return true;
}
4.addAll(int index, Collection<? extends E> collection)指定位置添加集合
@Override
public boolean addAll(int index, Collection<? extends E> collection) {
int s = size;
if (index > s || index < 0) {
throwIndexOutOfBoundsException(index, s);
}
Object[] newPart = collection.toArray();
int newPartSize = newPart.length;
if (newPartSize == 0) {
return false;
}
Object[] a = array;
int newSize = s + newPartSize; // If add overflows, arraycopy will fail
if (newSize <= a.length) { // 當前數(shù)組容量夠用, 直接拷貝
System.arraycopy(a, index, a, index + newPartSize, s - index);
} else {
int newCapacity = newCapacity(newSize - 1); // ~33% growth room
Object[] newArray = new Object[newCapacity];
System.arraycopy(a, 0, newArray, 0, index);
System.arraycopy(a, index, newArray, index + newPartSize, s-index);
array = a = newArray;
}
System.arraycopy(newPart, 0, a, index, newPartSize);
size = newSize;
modCount++;
return true;
}
前面都用到了這個關(guān)鍵的方法亦镶,理解了這個函數(shù)日月, 上面的大家都能看懂
* @param src
* 源數(shù)據(jù) , 一個數(shù)組
* @param srcPos
* 源數(shù)據(jù)開始的拷貝位置
* @param dst
* 目標數(shù)組
* @param dstPos
* 目標數(shù)據(jù)開始的放置位置 缤骨。
* @param length
* 拷貝長度 爱咬。
*/
public static native void arraycopy(Object src, int srcPos,
Object dst, int dstPos, int length);
其它的方法都比較簡單,大家可以自行看源碼绊起。
總結(jié)下:
1.ArrayList基于數(shù)組實現(xiàn)精拟,可以通過下標索引直接查找到指定位置的元素,因此查找效率高虱歪,但每次插入或刪除元素蜂绎,就要大量地移動元素,插入刪除元素的效率低
2.ArrayList不是線程安全的笋鄙。
3.ArrayList中允許元素為null师枣。