Java 2源码解读:java.util.ArrayList

来源:互联网 发布:默小宝的淘宝店 编辑:程序博客网 时间:2024/05/21 19:44

相关源码下载:
java.util.ArrayList
java.util.AbstractList
java.util.List

ArrayList是List接口的一个可变长数组实现。实现了所有List接口的操作,并允许存储null值。除了没有进行同步,ArrayList基本等同于Vector。在Vector中几乎对所有的方法都进行了同步,但ArrayList仅对writeObject和readObject进行了同步,其它比如add(Object)、remove(int)等都没有同步。

1.存储

ArrayList使用一个Object的数组存储元素。
private transient Object elementData[];
ArrayList实现了java.io.Serializable接口,这儿的transient标示这个属性不需要自动序列化。下面会在writeObject()方法中详细讲解为什么要这样作。

2.add和remove


  1.     public boolean add(Object o) {
  2.     ensureCapacity(size + 1);  // Increments modCount!!
  3.     elementData[size++] = o;
  4.     return true;
  5.     }

注意这儿的ensureCapacity()方法,它的作用是保证elementData数组的长度可以容纳一个新元素。在“自动变长机制”中将详细讲解。
  1.     public Object remove(int index) {
  2.     RangeCheck(index);
  3.     modCount++;
  4.     Object oldValue = elementData[index];
  5.     int numMoved = size - index - 1;
  6.     if (numMoved > 0)
  7.         System.arraycopy(elementData, index+1, elementData, index,
  8.                  numMoved);
  9.     elementData[--size] = null// Let gc do its work
  10.     return oldValue;
  11.     }

RangeCheck()的作用是进行边界检查。由于ArrayList采用一个对象数组存储元素,所以在删除一个元素时需要把后面的元素前移。删除一个元素时只是把该元素在elementData数组中的引用置为null,具体的对象的销毁由垃圾收集器负责。
modCount的作用将在下面的“iterator()中的同步”中说明。
注:在前移时使用了System提供的一个实用方法:arraycopy(),在本例中可以看出System.arraycopy()方法可以对同一个数组进行操作,这个方法是一个native方法,如果对同一个数组进行操作时,会首先把从源部分拷贝到一个临时数组,在把临时数组的元素拷贝到目标位置。

3.自动变长机制

在实例化一个ArrayList时,你可以指定一个初始容量。这个容量就是elementData数组的初始长度。如果你使用:
  1.     ArrayList list = new ArrayList();

则使用缺省的容量:10。
  1.     public ArrayList() {
  2.     this(10);
  3.     }

ArrayList提供了四种add()方法,
  • public boolean add(Object o)
  • public void add(int index, Object element)
  • public boolean addAll(Collection c)
  • public boolean addAll(int index, Collection c)

在每一种add()方法中,都首先调用了一个ensureCapacity(int miniCapacity)方法,这个方法保证elementData数组的长度不小于miniCapacity。ArrayList的自动变长机制就是在这个方法中实现的。
  1.     public void ensureCapacity(int minCapacity) {
  2.     modCount++;
  3.     int oldCapacity = elementData.length;
  4.     if (minCapacity > oldCapacity) {
  5.         Object oldData[] = elementData;
  6.         int newCapacity = (oldCapacity * 3)/2 + 1;
  7.             if (newCapacity < minCapacity)
  8.         newCapacity = minCapacity;
  9.         elementData = new Object[newCapacity];
  10.         System.arraycopy(oldData, 0, elementData, 0, size);
  11.     }
  12.     }

从这个方法实现中可以看出ArrayList每次扩容,都扩大到原来大小的1.5倍。
每种add()方法的实现都大同小异,下面给出add(Object)方法的实现:
  1.     public boolean add(Object o) {
  2.     ensureCapacity(size + 1);  // Increments modCount!!
  3.     elementData[size++] = o;
  4.     return true;
  5.     }

4.iterator()中的同步

在父类AbstractList中定义了一个int型的属性:modCount,记录了ArrayList结构性变化的次数。
  1.     protected transient int modCount = 0;

在ArrayList的所有涉及结构变化的方法中都增加modCount的值,包括:add()、remove()、addAll()、removeRange()及clear()方法。这些方法每调用一次,modCount的值就加1。
注:add()及addAll()方法的modCount的值是在其中调用的ensureCapacity()方法中增加的。

AbstractList中的iterator()方法(ArrayList直接继承了这个方法)使用了一个私有内部成员类Itr,生成一个Itr对象(Iterator接口)返回:
  1.     public Iterator iterator() {
  2.     return new Itr();
  3.     }

Itr实现了Iterator()接口,其中也定义了一个int型的属性:expectedModCount,这个属性在Itr类初始化时被赋予ArrayList对象的modCount属性的值。
  1.     int expectedModCount = modCount;

注:内部成员类Itr也是ArrayList类的一个成员,它可以访问所有的AbstractList的属性和方法。理解了这一点,Itr类的实现就容易理解了。

在Itr.hasNext()方法中:
  1.     public boolean hasNext() {
  2.         return cursor != size();
  3.     }

调用了AbstractList的size()方法,比较当前光标位置是否越界。

在Itr.next()方法中,Itr也调用了定义在AbstractList中的get(int)方法,返回当前光标处的元素:
  1.     public Object next() {
  2.         try {
  3.         Object next = get(cursor);
  4.         checkForComodification();
  5.         lastRet = cursor++;
  6.         return next;
  7.         } catch(IndexOutOfBoundsException e) {
  8.         checkForComodification();
  9.         throw new NoSuchElementException();
  10.         }
  11.     }

注意,在next()方法中调用了checkForComodification()方法,进行对修改的同步检查:
  1.     final void checkForComodification() {
  2.         if (modCount != expectedModCount)
  3.         throw new ConcurrentModificationException();
  4.     }

现在对modCount和expectedModCount的作用应该非常清楚了。在对一个集合对象进行跌代操作的同时,并不限制对集合对象的元素进行操作,这些操作包括一些可能引起跌代错误的add()或remove()等危险操作。在AbstractList中,使用了一个简单的机制来规避这些风险。这就是modCount和expectedModCount的作用所在。

5.序列化支持

ArrayList实现了java.io.Serializable接口,所以ArrayList对象可以序列化到持久存储介质中。ArrayList的主要属性定义如下:
  • private static final long serialVersionUID = 8683452581122892189L;
  • private transient Object elementData[];
  • private int size;

可以看出serialVersionUID和size都将自动序列化到介质中,但elementData数组对象却定义为transient了。也就是说ArrayList中的所有这些元素都不会自动系列化到介质中。为什么要这样实现?因为elementData数组中存储的“元素”其实仅是对这些元素的一个引用,并不是真正的对象,序列化一个对象的引用是毫无意义的,因为序列化是为了反序列化,当你反序列化时,这些对象的引用已经不可能指向原来的对象了。所以在这儿需要手工的对ArrayList的元素进行序列化操作。这就是writeObject()的作用。
  1.     private synchronized void writeObject(java.io.ObjectOutputStream s)
  2.         throws java.io.IOException{
  3.     // Write out element count, and any hidden stuff
  4.     s.defaultWriteObject();
  5.    // Write out array length
  6.     s.writeInt(elementData.length);
  7.     // Write out all elements in the proper order.
  8.     for (int i=0; i<size; i++)
  9.             s.writeObject(elementData[i]);
  10.     }

这样元素数组elementData中的所以元素对象就可以正确地序列化到存储介质了。
对应的readObject()也按照writeObject()方法的顺序从输入流中读取:
  1.     private synchronized void readObject(java.io.ObjectInputStream s)
  2.         throws java.io.IOExceptionClassNotFoundException {
  3.     // Read in size, and any hidden stuff
  4.     s.defaultReadObject();
  5.     // Read in array length and allocate array
  6.     int arrayLength = s.readInt();
  7.     elementData = new Object[arrayLength];
  8.     // Read in all elements in the proper order.
  9.     for (int i=0; i<size; i++)
  10.             elementData[i] = s.readObject();
  11.     }