1. 程式人生 > >java基礎,集合,Arraylist,源碼解析(基礎)

java基礎,集合,Arraylist,源碼解析(基礎)

range 都沒有 elements 解析 close buffer 區別 ati copy

ArrayList

  • 是什麽,定義?

這是動態的數組,它提供了動態的增加和減少元素,實現了List接口(List實現Collection,所以也實現Collection接口)靈活的設置數組的大小等好處

  

  • 內部如何實現
 1     /**
 2      * The array buffer into which the elements of the ArrayList are stored.
 3      * The capacity of the ArrayList is the length of this array buffer.
 4      */
 5     private
transient Object[] elementData;//內部是數組 6 7 /** 8 * The size of the ArrayList (the number of elements it contains). 9 * 10 * @serial 11 */ 12 private int size;//定義大小 13 14 /** 15 * Constructs an empty list with the specified initial capacity. 16 * 17 * @param
initialCapacity the initial capacity of the list 18 * @throws IllegalArgumentException if the specified initial capacity 19 * is negative 20 */ 21 public ArrayList(int initialCapacity) {//帶參數的初始化 22 super(); 23 if (initialCapacity < 0) 24 throw
new IllegalArgumentException("Illegal Capacity: "+ 25 initialCapacity); 26 this.elementData = new Object[initialCapacity]; 27 } 28 29 /** 30 * Constructs an empty list with an initial capacity of ten. 31 */ 32 public ArrayList() {//默認初始化,大小是10 33 this(10); 34 }

  • 添加如何操作
 1   public boolean add(E e) {
 2         ensureCapacityInternal(size + 1);  // 進行長度的判斷與修改(將檢查與擴容放在一個方法調用,代碼閱讀性高)
 3         elementData[size++] = e;
 4         return true;
 5     }
 6   
 7     private void ensureCapacityInternal(int minCapacity) {
 8         modCount++;
 9         // overflow-conscious code
10         if (minCapacity - elementData.length > 0)//進行判斷
11             grow(minCapacity);
12     }
13 
14     private void grow(int minCapacity) {
15         // overflow-conscious code
16         int oldCapacity = elementData.length;
17         int newCapacity = oldCapacity + (oldCapacity >> 1);//擴大原來的二分之一
18         if (newCapacity - minCapacity < 0)//最小的長度
19             newCapacity = minCapacity;
20         if (newCapacity - MAX_ARRAY_SIZE > 0)//最大長度
21             newCapacity = hugeCapacity(minCapacity);
22         // minCapacity is usually close to size, so this is a win:
23         elementData = Arrays.copyOf(elementData, newCapacity);
24     }
  • 添加重要的一部,擴容,——這是為什麽實際應用中要定義數組大小的原因,
 1    //Array的源代碼
 2 public static <T> T[] copyOf(T[] original, int newLength) {
 3         return (T[]) copyOf(original, newLength, original.getClass());
 4     }
 5 
 6     public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {//需要復制原來的數組
 7         T[] copy = ((Object)newType == (Object)Object[].class)
 8             ? (T[]) new Object[newLength]
 9             : (T[]) Array.newInstance(newType.getComponentType(), newLength);
10         System.arraycopy(original, 0, copy, 0,
11                          Math.min(original.length, newLength));
12         return copy;
13     }
  • 制定位置的添加
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     }
  • 刪除對象
    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;
    }
  • 怎麽遍歷呢?

數組,通過數組的腳標,遍歷;

for each 因為實現Iterator,也可以使用叠代器

  

使用中改註意:

1、初始化最好定義都用的長度,避免Arrarlist內部復制的操作;

2、如果刪除的,盡量從後往前刪除

3、不是線程安全,全部代碼中都沒有synchronized ,

  如果有需要,使用Vector,兩者的區別3點(其他基本是一樣):

  線程安全,

  多了方法indexOf,

  擴容的打大小是:1倍

      int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
      capacityIncrement : oldCapacity);

  

java基礎,集合,Arraylist,源碼解析(基礎)