public E remove(int index) {
//检查下标
rangeCheck(index);
//modCount加一
modCount++;
//取出要删除位置的元素,用来做返回值
E oldValue = elementData(index);
//将要删除位置之后的元素向前移动一个位,如果是最后一个元素,numMoved==0,直接将elementData[size]置空即可
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // 将引用置空,让GC回收
return oldValue;
}
2.删除指定元素
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;
}
/**
* 私有的 移除 方法 跳过边界检查且不返回移除的元素
* @param index
*/
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//将引用置空,让GC回收
elementData[--size] = null; // clear to let GC do its work
}
查找方法
1.查找指定元素的位置
/**
* 返回指定元素的第一次出现的位置,如果没有,返回-1。
* @param o 要查找的元素
* @return
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
/**
* 保存ArrayList的状态到一个流(序列化)
* @param s
* @throws java.io.IOException
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out size as capacity for behavioural compatibility with clone()
s.writeInt(size);
// Write out all elements in the proper order.
for (int i=0; i<size; i++) {
s.writeObject(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
2.反序列化
/**
* 重建ArrayList实例通过一个流(反序列化)
* @param s
* @throws java.io.IOException
* @throws ClassNotFoundException
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
elementData = EMPTY_ELEMENTDATA;
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in capacity
s.readInt(); // ignored
if (size > 0) {
// be like clone(), allocate array based upon size not capacity
ensureCapacityInternal(size);
Object[] a = elementData;
// Read in all elements in the proper order.
for (int i=0; i<size; i++) {
a[i] = s.readObject();
}
}
}
public Object clone() {
try {
ArrayList<?> v = (ArrayList<?>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
ArrayList的克隆是浅克隆,将全部元素复制到一个新数组返回。
迭代器
1.创建迭代器方法
/**
* 返回的迭代器是“快速失败”的
* @return 一个按适当的顺序遍历列表中元素的迭代器
*/
public Iterator<E> iterator() {
return new Itr();
}
2.Itr类的属性
private class Itr implements Iterator<E> {
int cursor; //下一个要返回的元素的下标
int lastRet = -1; //最后一个要返回元素的下标,如果没有返回 -1
int expectedModCount = modCount;//期望的modCount
}
3.Itr类的next方法和hasNext方法
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
//调用next()方法会检查对象有没有发生结构性变化,即expectedModCount等不等于modCount
//如果不等于就会抛出ConcurrentModificationException异常
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
4.Itr类的remove方法
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
//调用迭代器的remove()方法来删除数据可以避免抛出 ConcurrentModificationException 异常,这里会修改expectedModCount
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}