天天看點

JDK7集合架構源碼學習-ArrayList(1)

整個ArrayList用的最多的就是System類提供的本地方法.

public static void arraycopy(Object src,
             int srcPos, Object dest,
             int destPos,
             int length)      

和JDK6不一樣,JDK7的ArrayList構造函數,建立的是一個沒有任何元素的數組.而JDK6預設建立10個元素的數組.

新增元素流程

  1. public boolean add(E e) {  
  2.     ensureCapacityInternal(size + 1);  // Increments modCount!!  
  3.     elementData[size++] = e;  
  4.     return true;  
  5. }  
  6. private void ensureCapacityInternal(int minCapacity) {  
  7.     if (elementData == EMPTY_ELEMENTDATA) {  
  8.         minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);  
  9.     }  
  10.     ensureExplicitCapacity(minCapacity);  
  11. }  
  12. private void ensureExplicitCapacity(int minCapacity) {  
  13.     modCount++;  
  14.     // overflow-conscious code  
  15.     if (minCapacity - elementData.length > 0)  
  16.         grow(minCapacity);  
  17. }  
  18. private void grow(int minCapacity) {  
  19.     // overflow-conscious code  
  20.     int oldCapacity = elementData.length;  
  21.     int newCapacity = oldCapacity + (oldCapacity >> 1);  
  22.     if (newCapacity - minCapacity < 0)  
  23.         newCapacity = minCapacity;  
  24.     if (newCapacity - MAX_ARRAY_SIZE > 0)  
  25.         newCapacity = hugeCapacity(minCapacity);  
  26.     // minCapacity is usually close to size, so this is a win:  
  27.     elementData = Arrays.copyOf(elementData, newCapacity);  
  28. }  

add之前,先保證容量.如果數組是空的,則配置設定10個元素.否則增長目前數組容量的一半.

其他的方法也是類似

  1. public void add(int index, E element) {  
  2.     rangeCheckForAdd(index);  
  3.     ensureCapacityInternal(size + 1);  // Increments modCount!!  
  4.     System.arraycopy(elementData, index, elementData, index + 1,  
  5.                      size - index);  
  6.     elementData[index] = element;  
  7.     size++;  
  8. }  
  9. public E remove(int index) {  
  10.     rangeCheck(index);  
  11.     modCount++;  
  12.     E oldValue = elementData(index);  
  13.     int numMoved = size - index - 1;  
  14.     if (numMoved > 0)  
  15.         System.arraycopy(elementData, index+1, elementData, index,  
  16.                          numMoved);  
  17.     elementData[--size] = null; // clear to let GC do its work  
  18.     return oldValue;  
  19. }  
  20. public boolean remove(Object o) {  
  21.     if (o == null) {  
  22.         for (int index = 0; index < size; index++)  
  23.             if (elementData[index] == null) {  
  24.                 fastRemove(index);  
  25.                 return true;  
  26.             }  
  27.     } else {  
  28.         for (int index = 0; index < size; index++)  
  29.             if (o.equals(elementData[index])) {  
  30.                 fastRemove(index);  
  31.                 return true;  
  32.             }  
  33.     }  
  34.     return false;  
  35. }  
  36. public boolean addAll(Collection<!--? extends E> c) {  
  37.     Object[] a = c.toArray();  
  38.     int numNew = a.length;  
  39.     ensureCapacityInternal(size + numNew);  // Increments modCount  
  40.     System.arraycopy(a, 0, elementData, size, numNew);  
  41.     size += numNew;  
  42.     return numNew != 0;  
  43. }  

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2121444/,如需轉載,請注明出處,否則将追究法律責任。

轉載于:http://blog.itpub.net/29254281/viewspace-2121444/