ArrayList ArrayList是一个可变数组实现,实现了List接口的所有方法,并允许存取null值.ArrayList基本上等同与Vector,但它只对writeObject()和readObject()进行了同步. 1.序列化 ArrayList使用一个Object的数组存储元素. private transient E[] elementData; ArrayList实现了Java.io.Serializable接口,这里的transient标示这个属性不需自动序列化.这是因为elementData数组中存储的"元素"其实只是对这些元素的一个引用,并不是真正的对象,序列化没有意义.因为序列化是为了反序列化,当你反序列化时,这些对象的引用已经不可能指向原来的对象了.所以要手工对ArrayList的元素进行序列化操作,这就是writeObject()的作用. private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{ // Write out element count, and any hidden stuff s.defaultWriteObject();
// Write out array length s.writeInt(elementData.length);
// Write out all elements in the proper order. for (int i=0; i<size; i++) s.writeObject(elementData[i]); } 对应的readObject()为: private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in size, and any hidden stuff s.defaultReadObject();
// Read in array length and allocate array int arrayLength = s.readInt(); Object[] a = elementData = (E[])new Object[arrayLength];
// Read in all elements in the proper order. for (int i=0; i<size; i++) a[i] = s.readObject(); } 2.自动变长机制 在每一个add() 方法中,都首先调用一个ensureCapacity(int miniCapacity)方法,这个方法保证elementData数组的长度不小于miniCapacity,ArrayList的自动变长机制就是在这个方法中实现的. public void ensureCapacity(int minCapacity) { modCount++; int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; elementData = (E[])new Object[newCapacity]; System.arraycopy(oldData, 0, elementData, 0, size); } } 从这个方法可以看出ArrayList每次扩容都扩大到原来的1.5倍. 每种add()方法的实现都大同小异,下面给出add(Object)的实现: public boolean add(E o) { ensureCapacity(size + 1); // Increments modCount!! elementData[size++] = o; return true; }
remove()方法
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; // Let gc do its work
return oldValue; } RangeCheck()的作用是进行边界检查.出于ArrayList采用一个对象数组元素存储元素,所以删除一个元素时需要把后面的元素前移.删除一个元素时只是把该元素在elementData数组中的引用置为null. modCount的作用将在下面的"interator()中的同步"中说明. 注:在前移时使用了System.arrayCopy(),这个方法是native方法,如是对同一个数组进行操作,会首先从中标位置. 3.快速失败迭代器 在父类AbstractList中定义了一个int型的属性:modCount,记录了ArrayList结构性变化的次数. protected transient int modCount = 0; 在ArrayList的所有涉及结构变化的方法中都增加modCount的值,包括:add(),remove(),addAll(),removeRange()及clear()方法.这些方法每调用一次,modCount的值就会加1. 注:add()及addAll()方法的modCount的值是在其中调用的ensureCapacity()方法中增加的. AbstractList中的iterator()方法(ArrayList直接继承了这个方法)使用了一个私有内部成员类Itr,生成一个Itr对象(Iterator接口),返回: public Iterator<E> iterator() { return listIterator(); } Itr实现了Iterator()接口,其中也定义了一个int型的属性:expectedModCount,这个属性在Itr类初始化时被赋予ArrayList对象的modCount属性的值. 注:内部成员类Itr也是ArrayList类的一个成员,它可以访问所有的AbstractList的属性和方法. 在Itr.next()方法中,Itr也调用了定义在AbstractList中的get(int)方法,返回当前光标的元素: public E next() { checkForComodification(); try { E next = get(cursor); lastRet = cursor++; return next; } catch(IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } 注意,在next()方法中调用了checkForComodification()方法,进行对修改的同步检查: final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } 现在对modCount和expectedModCount的作用应该非常清楚了.在对一个元素集合对象进行迭代操作时,并不是限制对集合对象元素进行操作,这些操作包括一些可能引起迭代错误的add()或remove()等危险操作.在AbstractList中,使用了一个简单的机制来规避这些风险.这就是modCount和expectedModCount的作用所在.