java1.7セットソース読み:ArrayList

9730 ワード

まずクラス定義を見てみましょう.
1 public class ArrayList<E> extends AbstractList<E>
2         implements List<E>, RandomAccess, Cloneable, java.io.Serializable
3 {
4   ...........  
5 }
ArrayList  List  ,     Collection,     Iterable,      Cloneable、Serializable、RandomAccess  ,     、   、    。
ArrayList     10,                             ,        :
1     private transient Object[] elementData;
2     ......
3   public ArrayList(int initialCapacity) {
4         super();
5         if (initialCapacity < 0)
6             throw new IllegalArgumentException("Illegal Capacity: "+
7                                                initialCapacity);
8         this.elementData = new Object[initialCapacity];
9     }
ArrayList           ,  add、get                ,                ,           ,           .
:
 1   private void rangeCheck(int index) {
 2         if (index >= size)
 3             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 4     }
 5 
 6     /**
 7      * A version of rangeCheck used by add and addAll.
 8      */
 9     private void rangeCheckForAdd(int index) {
10         if (index > size || index < 0)
11             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
12     }

容量の検証と拡張:
 1   /**
 2      * Increases the capacity of this <tt>ArrayList</tt> instance, if
 3      * necessary, to ensure that it can hold at least the number of elements
 4      * specified by the minimum capacity argument.
 5      *
 6      * @param   minCapacity   the desired minimum capacity
 7      */
 8     public void ensureCapacity(int minCapacity) {
 9         int minExpand = (elementData != EMPTY_ELEMENTDATA)
10             // any size if real element table
11             ? 0
12             // larger than default for empty table. It's already supposed to be
13             // at default size.
14             : DEFAULT_CAPACITY;
15 
16         if (minCapacity > minExpand) {
17             ensureExplicitCapacity(minCapacity);
18         }
19     }
20 
21     private void ensureCapacityInternal(int minCapacity) {
22         if (elementData == EMPTY_ELEMENTDATA) {
23             minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
24         }
25 
26         ensureExplicitCapacity(minCapacity);
27     }
28 
29     private void ensureExplicitCapacity(int minCapacity) {
30         modCount++;
31 
32         // overflow-conscious code
33         if (minCapacity - elementData.length > 0)
34             grow(minCapacity);
35     }
36     private void grow(int minCapacity) {
37         // overflow-conscious code
38         int oldCapacity = elementData.length;
39         int newCapacity = oldCapacity + (oldCapacity >> 1);
40         if (newCapacity - minCapacity < 0)
41             newCapacity = minCapacity;
42         if (newCapacity - MAX_ARRAY_SIZE > 0)
43             newCapacity = hugeCapacity(minCapacity);
44         // minCapacity is usually close to size, so this is a win:
45         elementData = Arrays.copyOf(elementData, newCapacity);
46     }

拡張時は、元の容量の1.5倍です.
ArrayList対Iteratorインタフェースの実装:
 1   /**
 2      * An optimized version of AbstractList.Itr
 3      */
 4     private class Itr implements Iterator<E> {
 5         int cursor;       // index of next element to return
 6         int lastRet = -1; // index of last element returned; -1 if no such
 7         int expectedModCount = modCount;
 8 
 9         public boolean hasNext() {
10             return cursor != size;
11         }
12 
13         @SuppressWarnings("unchecked")
14         public E next() {
15             checkForComodification();
16             int i = cursor;
17             if (i >= size)
18                 throw new NoSuchElementException();
19             Object[] elementData = ArrayList.this.elementData;
20             if (i >= elementData.length)
21                 throw new ConcurrentModificationException();
22             cursor = i + 1;
23             return (E) elementData[lastRet = i];
24         }
25 
26         public void remove() {
27             if (lastRet < 0)
28                 throw new IllegalStateException();
29             checkForComodification();
30 
31             try {
32                 ArrayList.this.remove(lastRet);
33                 cursor = lastRet;
34                 lastRet = -1;
35                 expectedModCount = modCount;
36             } catch (IndexOutOfBoundsException ex) {
37                 throw new ConcurrentModificationException();
38             }
39         }
40 
41         final void checkForComodification() {
42             if (modCount != expectedModCount)
43                 throw new ConcurrentModificationException();
44         }
45     }

ArrayListに対してforループを行った場合、集合を添削する操作がある場合、ConcurrentModificationException異常が発生すると報告されます.
特に注意:ArrayListはスレッドセキュリティではなく、スレッドセキュリティの問題がある場合はVectorを使用します.