容器初始化方法:
兩種初始化方法
- 不指定初始容器大小
ArrayList<String> arrayList = new ArrayList<String>();
內(nèi)部源碼操作
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
EMPTY_ELEMENTDATA 是一個空的Object類型的數(shù)組
private static final Object[] EMPTY_ELEMENTDATA = {};
- 指定初始化容器大小
ArrayList<String> arrayList = new ArrayList<String>(2);
//如果是指定初始化大小,內(nèi)部會把this.elementData = new Object[initialCapacity];這個參數(shù)初始化一個大小
添加方法
有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會記錄當(dāng)前添加的時第幾個,然后下一個是在哪個位置
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);//如果容器沒有指定初始化大小,則初始化一個最小的,jdk1.7里面指定的最小的大小是10
}
ensureExplicitCapacity(minCapacity);//保證數(shù)組容器大小,
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)//判斷是否需要擴(kuò)容
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1); //每次擴(kuò)容增加原來的一半
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); //內(nèi)部使用了System.arraycopy復(fù)制數(shù)組
}
add(int index ,E e )源碼:
public void add(int index, E element) {
rangeCheckForAdd(index);//檢查是否越界
ensureCapacityInternal(size + 1); // 先擴(kuò)容,保證后面復(fù)制的時候需要的長度
System.arraycopy(elementData, index, elementData, index + 1,
size - index); //主要是這里調(diào)用了api復(fù)制數(shù)組,移動index位置到后面一個位置,留出位置放
elementData[index] = element;放數(shù)據(jù)
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方法 根據(jù)下標(biāo)移除也差不多,removeAll也差不多
}
獲取方法
get(int index):直接數(shù)組下標(biāo)取值