ITPub博客

首页 > 应用开发 > Java > JDK7集合框架源码学习-ArrayList(1)

JDK7集合框架源码学习-ArrayList(1)

原创 Java 作者:壹頁書 时间:2016-07-04 14:49:21 0 删除 编辑
整个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.   
  7. private void ensureCapacityInternal(int minCapacity) {  
  8.     if (elementData == EMPTY_ELEMENTDATA) {  
  9.         minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);  
  10.     }  
  11.   
  12.     ensureExplicitCapacity(minCapacity);  
  13. }  
  14.   
  15. private void ensureExplicitCapacity(int minCapacity) {  
  16.     modCount++;  
  17.   
  18.     // overflow-conscious code  
  19.     if (minCapacity - elementData.length > 0)  
  20.         grow(minCapacity);  
  21. }  
  22. private void grow(int minCapacity) {  
  23.     // overflow-conscious code  
  24.     int oldCapacity = elementData.length;  
  25.     int newCapacity = oldCapacity + (oldCapacity >> 1);  
  26.     if (newCapacity - minCapacity < 0)  
  27.         newCapacity = minCapacity;  
  28.     if (newCapacity - MAX_ARRAY_SIZE > 0)  
  29.         newCapacity = hugeCapacity(minCapacity);  
  30.     // minCapacity is usually close to size, so this is a win:  
  31.     elementData = Arrays.copyOf(elementData, newCapacity);  
  32. }  

add之前,先保证容量.如果数组是空的,则分配10个元素.否则增长目前数组容量的一半.

其他的方法也是类似
  1. public void add(int index, E element) {  
  2.     rangeCheckForAdd(index);  
  3.   
  4.     ensureCapacityInternal(size + 1);  // Increments modCount!!  
  5.     System.arraycopy(elementData, index, elementData, index + 1,  
  6.                      size - index);  
  7.     elementData[index] = element;  
  8.     size++;  
  9. }  

  10. public E remove(int index) {  
  11.     rangeCheck(index);  
  12.   
  13.     modCount++;  
  14.     E oldValue = elementData(index);  
  15.   
  16.     int numMoved = size - index - 1;  
  17.     if (numMoved > 0)  
  18.         System.arraycopy(elementData, index+1, elementData, index,  
  19.                          numMoved);  
  20.     elementData[--size] = null; // clear to let GC do its work  
  21.   
  22.     return oldValue;  
  23. }  

  24. public boolean remove(Object o) {  
  25.     if (o == null) {  
  26.         for (int index = 0; index < size; index++)  
  27.             if (elementData[index] == null) {  
  28.                 fastRemove(index);  
  29.                 return true;  
  30.             }  
  31.     } else {  
  32.         for (int index = 0; index < size; index++)  
  33.             if (o.equals(elementData[index])) {  
  34.                 fastRemove(index);  
  35.                 return true;  
  36.             }  
  37.     }  
  38.     return false;  
  39. }  

  40. public boolean addAll(Collection<!--? extends E> c) {  
  41.     Object[] a = c.toArray();  
  42.     int numNew = a.length;  
  43.     ensureCapacityInternal(size + numNew);  // Increments modCount  
  44.     System.arraycopy(a, 0, elementData, size, numNew);  
  45.     size += numNew;  
  46.     return numNew != 0;  
  47. }  

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2121444/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339915