1. 程式人生 > 實用技巧 >3.4 ArrayList 類的實現

3.4 ArrayList 類的實現

3.4 ArrayList 類的實現

這節提供一個便於使用的 ArrayList 泛型類的實現,這裡不檢測可能使得迭代器無效的結構上的修改,也不檢測非法的迭代器 remove 方法。

  1. MyArrayList 將保持基礎陣列,陣列的容量,以及儲存在MyArrayList 中的當前項數。
  2. MyArrayList 將提供一種機制以改變基礎陣列的容量。通過獲得一個新陣列,將老陣列拷貝到新陣列中來改變陣列的容量,允許虛擬機器回收老陣列。
  3. MyArrayList 將提供 get 和 set 的實現。
  4. MyArrayList 將提供基本的例程,如 size 、isEmpty 和 clear,它們是典型的單行程式;還提供 remove,以及兩種不同版本的 add。如果陣列大小和容量相同,那麼這兩個 add 例程將增加容量。
  5. MyArrayList 將提供一個實現 Iterator 介面的類。這個類將儲存迭代序列中的下一項的下標,並提供 next、hasNext 和 remove 等方法的實現。MyArrayList 的迭代器方法直接返回實現 Iterator 介面的該類的新構造的例項。
public class MyArrayList<E> implements Iterable<E> {

    public static final int DEFAULT_CAPACITY = 10;//預設陣列長度

    private int theSize;

    private E[] theItems;

    public MyArrayList() {
        doClear();
    }

    public void clear() {
        doClear();
    }

    private void doClear() {
        theSize = 0;
        ensureCapacity(DEFAULT_CAPACITY);
    }

    public int size() {
        return theSize;
    }

    public boolean isEmpty() {
        return theSize == 0;
    }

    public void trimToSize() {//將當前陣列複製到一個大小剛好合適的陣列中
        ensureCapacity(theSize);
    }

    public E get(int idx) {
        if (idx < 0 || idx >= theSize) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return theItems[idx];
    }

    public E set(int idx, E newVal) {
        if (idx < 0 || idx >= theSize) {
            throw new ArrayIndexOutOfBoundsException();
        }
        E old = theItems[idx];
        theItems[idx] = newVal;
        return old;
    }

    public void ensureCapacity(int newCapacity) {
        if (newCapacity < theSize) {
            return;
        }
        E[] old = theItems;
        theItems = (E[]) new Object[newCapacity];
        for (int i = 0; i < theSize; i++) {
            theItems[i] = old[i];
        }
    }

    public boolean add(E x) {
        add(theSize, x);
        return true;
    }

    public void add(int idx, E x) {
        if (theItems.length == theSize) {
            ensureCapacity(theSize * 2 + 1);//+1應對大小為0時的情況
        }
        for (int i = theSize; i > idx; i--) {
            theItems[i] = theItems[i - 1];
        }
        theItems[idx] = x;
        theSize++;
    }

    public E remove(int idx) {
        E removedItem = theItems[idx];
        for (int i = idx; i < theSize; i++) {
            theItems[i] = theItems[i + 1];
        }
        theSize--;
        return removedItem;
    }

    public Iterator<E> iterator() {
        return new ArrayListIterator();
    }

    private class ArrayListIterator implements Iterator<E> {

        private int current = 0;

        public boolean hasNext() {
            return current < size();
        }

        public E next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return theItems[current++];
        }

        public void remove() {
            MyArrayList.this.remove(--current);
        }
    }
}

ArrayListIterator 是 MyArrayList 的內部類,可以直接訪問 MyArrayList 的 private 例項