本文只是学习笔记,如有错误,欢迎指出
ArrayList是使用一个elementData对象数组来存储元素的
private transient Object[] elementData;
构造器很简单,就是根据传入的参数初始化elementData数组,默认大小为10
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
get操作
public E get(int index) {
//数组越界则抛异常
rangeCheck(index);
//返回elementData数组下标为index的元素
return elementData(index);
}
E elementData(int index) {
return (E) elementData[index];
}
add操作
public boolean add(E e) {
//确保容量足够添加新元素
ensureCapacity(size + 1); // Increments modCount!!
//添加到数组size++位置
elementData[size++] = e;
return true;
}
//如果添加元素会超过数组大小,则扩展容量
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
//需要的最小容量大于数组大小
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
//新的数组大小为原来的3/2+1
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
//创建一个新的数组,把旧的元素复制到新的数组上
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
删除操作
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
//判断删除下标为index的元素后,需要移动被删除元素后面的数组到前一个位置上
int numMoved = size - index - 1;
//如果删除的不是最后一个元素
if (numMoved > 0)
//将index+1后面的元素复制到index开始的位置上
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//将最后一个元素置为空
elementData[--size] = null; // Let gc do its work
return oldValue;
}
删除列表中第一个为o的元素
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
//遍历数组,判断是否等于null
if (elementData[index] == null) {
//删除下标index的元素
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
//遍历数组,判断是否等于o
if (o.equals(elementData[index])) {
//删除下标index的元素
fastRemove(index);
return true;
}
}
return false;
}
和remove(int index)类似,知识不返回被删除的元素
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; // Let gc do its work
}
获取子列表,修改子列表的元素会影响ArrayList
这样可以用来删除从from到to-1下标的元素
list.subList(from, to).clear();
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
//返回的是一个SubList类
return new SubList(this, 0, fromIndex, toIndex);
}
SubList继承AbstractList
private class SubList extends AbstractList<E> implements RandomAccess
//父ArrayList
private final AbstractList<E> parent;
//相对于父List的偏移量
private final int parentOffset;
构造器
SubList(AbstractList<E> parent,
int offset, int fromIndex, int toIndex) {
this.parent = parent;
//相对于父List的偏移量parentOffset也即说传入的开始位置fromIndex
this.parentOffset = fromIndex;
this.offset = offset + fromIndex;
this.size = toIndex - fromIndex;
//保存父List的modCount,不正确的修改会抛异常
this.modCount = ArrayList.this.modCount;
}
get操作
public E get(int index) {
//检查是否越界
rangeCheck(index);
//检查ArrayList是否被结构性修改了,例如调用了add、remove等方法
checkForComodification();
//调用ArrayList.elementData方法来获取元素
return ArrayList.this.elementData(offset + index);
}
add操作
public void add(int index, E e) {
rangeCheckForAdd(index);
checkForComodification();
//调用ArrayList.add
parent.add(parentOffset + index, e);
//修改modCount
this.modCount = parent.modCount;
this.size++;
}
remove操作
public E remove(int index) {
rangeCheck(index);
checkForComodification();
//调用ArrayList.remove
E result = parent.remove(parentOffset + index);
//修改modCount
this.modCount = parent.modCount;
this.size--;
return result;
}