1. 程式人生 > >Java陣列--二次封裝(一)

Java陣列--二次封裝(一)

使用Java中的陣列二次封裝屬於我們的陣列

  1. 實現增加元素、刪除元素、修改元素、搜尋元素功能。
  2. 使用泛型。
  3. 支援動態擴容。
/**
 * 通過對陣列封裝實現自己的Array類
 */
public class Array<E> {

    //定義一個整型的一維陣列的成員變數
    private E[] data;
    //陣列中元素的個數,手動去維繫它
    private int size;

    /**
     * 無參建構函式
     */
    public Array() {
        this(10);
    }

    /**
     * 有參建構函式
     *
     * @param capacity 陣列的容量,預設為10
     */
    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    /**
     * 獲取陣列中的元素個數
     *
     * @return 陣列中元素的個數
     */
    public int getSize() {
        return size;
    }

    /**
     * 獲取陣列的容量大小
     *
     * @return 陣列的容量大小
     */
    public int getCapacity() {
        return data.length;
    }

    /**
     * 獲取陣列是否為空
     *
     * @return 陣列是否為空,true為空,false不為空
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 向陣列中某個位置新增一個新的元素
     *
     * @param index 待新增的索引座標
     * @param e     待新增的新元素
     */
    public void add(int index, E e) {

        if (index < 0 || index > size)
            throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");

        if (size - data.length >= 0) {
            int newCapacity = data.length + (data.length >> 1);
            resize(newCapacity);
        }

        for (int i = size - 1; i >= index; i--)
            data[i + 1] = data[i];

        data[index] = e;
        size++;
    }

    /**
     * 向陣列中所有元素前新增一個新的元素
     *
     * @param e 新增的元素
     */
    public void addFrist(E e) {
        add(0, e);
    }

    /**
     * 向陣列中所有元素後新增一個新的元素
     *
     * @param e 新增的元素
     */
    public void addLast(E e) {
        add(size, e);
    }

    /**
     * 獲取某個索引座標的元素值
     *
     * @param index 索引座標
     * @return 索引座標對應的元素值
     */
    public E get(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal.");
        return data[index];
    }

    /**
     * 獲取第一個索引座標的元素值
     *
     * @return 索引座標對應的元素值
     */
    public E getFrist() {
        return get(0);
    }

    /**
     * 獲取最後一個索引座標的元素值
     *
     * @return 索引座標對應的元素值
     */
    public E getLast() {
        return get(size - 1);
    }

    /**
     * 修改index索引座標的元素e
     *
     * @param index 索引座標
     * @param e     修改的元素值
     */
    public void set(int index, E e) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("set failed. Index is illegal.");
        data[index] = e;
    }

    /**
     * 檢查當前陣列是否包含元素e
     *
     * @param e 要包含的元素
     * @return true代表包含,false代表不包含
     */
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return true;
        }
        return false;
    }

    /**
     * 查詢陣列中元素e的索引座標
     *
     * @param e 要查詢的元素
     * @return 存在則返回元素e對應的索引座標,不存在則返回-1
     */
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return i;
        }
        return -1;
    }

    /**
     * 從陣列中刪除index位置的元素
     *
     * @param index 待刪除的索引座標
     * @return 刪除的元素
     */
    public E remove(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Remove failed. Index is illegal.");

        E ret = data[index];
        for (int i = index + 1; i < size; i++)
            data[i - 1] = data[i];

        size--;
        data[size] = null;
        
        return ret;
    }

    /**
     * 從陣列中刪除第一個元素
     *
     * @return 刪除的元素
     */
    public E removeFrist() {
        return remove(0);
    }

    /**
     * 從陣列中刪除最後一個元素
     *
     * @return 刪除的元素
     */
    public E removeLast() {
        return remove(size - 1);
    }

    /**
     * 從陣列中刪除元素e
     * 但是這裡注意,這裡刪除的e只是元素e第一次出現在陣列中的位置,
     * 如果後續有相同的元素e則不會刪除
     *
     * @param e 待刪除的元素e
     */
    public void removeElement(E e) {
        int index = find(e);
        if (index != -1)
            remove(index);
    }

    /**
     * 動態陣列自動擴容
     *
     * @param newCapactity 陣列的容度大小
     */
    public void resize(int newCapactity) {
        E[] newData = (E[]) new Object[newCapactity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
        newData = null;
    }

    /**
     * 獲取String型別結果
     * 注意:@Override 代表的是複用父類的方法
     *
     * @return 輸出String型別結果
     */
    @Override
    public String toString() {
        StringBuffer res = new StringBuffer();
        res.append("Array:[");
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1)
                res.append(", ");
        }
        res.append(']');
        res.append(String.format(" , size = %d , capacity = %d", size, data.length));
        return res.toString();
    }


}