目录
addAll(int index, Collection c)
removeRange(int fromIndex, int toIndex)
remove(int index)
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;
}
作用:删除索引位置处的元素。
先检查索引是否合法,然后modCount加1。然后获取index位置上原有的元素的值,最后返回该值。
通过System.arraycopy方法,将ArrayList的数组中index位置的后一位到数组结束的所有元素都向左移动一位,同时将数组的最后一个位置设置为null,方便GC进行回收。
remove(Object o)
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;
}
作用:删除ArrayList中值等于Object o的第一元素。
当Object o为null时,遍历数组,当遇到第一个值为null的时候,调用fastRemove删除该值,并将所有后面的元素往前移动一位。
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; // clear to let GC do its work
}
fastRemove的作用于前面的remove(int index)方法作用类似,只是少了参数的校验和返回值。
当Object o不为null时,遍历数组,当遇到第一个值等于Object o的时候,调用fastRemove删除该值,并将所有后面的元素往前移动一位。
clear()
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
作用:清空ArrayList中的所有元素。
遍历数组,将所有位置的值都设置成null,便于GC,同时将ArrayList元素个数归0。
addAll(Collection<? extends E> 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;
}
作用:将Collection c中的元素加入ArrayList的末尾。
先将Collection c转换成数组,再将Collection c中所有元素个数加上ArrayList元素的个数,与数组的容量对比,根据结果来判断数组是否需要扩容。然后利用native方法System.arraycopy(),将Collection c的元素复制到ArrayList的尾部,ArrayList元素的个数size要加上Collection c的元素个数。
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;
}
作用:将Collection c中的元素加入ArrayList中指定的位置上。
先使用rangeCheckForAdd()校验索引的合法性。再将Collection c转换成数组,再将Collection c中所有元素个数加上ArrayList元素的个数,与数组的容量对比,根据结果来判断数组是否需要扩容。然后利用native方法System.arraycopy(),将数组中index索引处往后数c.length个元素,往后移动。然后将Collection c的元素放入数组中空出来的那些位置上。
removeRange(int fromIndex, int toIndex)
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// clear to let GC do its work
int newSize = size - (toIndex-fromIndex);
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}
作用:删除ArrayList中两个位置中间的所有元素。
将数组中从toIndex位置到结尾处的所有元素,移动到fromIndex位置处。然后将数组中从末尾往前数,数出(toIndex-fromIndex)个位置,将他们设置成null,便于GC。新ArrayList的元素个数重新赋值。