JAVA數據結構---動態擴展內存自定義ArrayList

代碼分爲2個部分:
向量類:MyArrayList.java
測試類:MainTest.java


向量類:MyArrayList.java

@SuppressWarnings("unchecked")
public class MyArrayList<T> {
    private T[] data;// 泛型數據
    private int count;// 加入的元素個數
    private final int DEFLAUT_SIZE = 10;// 默認的數組長度
    private int listSize;// 實例化數組的長度

    /**
     * 構造函數:有參數,無參數
     */

    public MyArrayList(int oldSize) {
        data = (T[]) (new Object[oldSize]);
        count = 0;
        listSize = oldSize;
    }

    public MyArrayList() {
        data = (T[]) (new Object[DEFLAUT_SIZE]);
        count = 0;
        listSize = DEFLAUT_SIZE;
    }

    /**
     * 私有的方法
     */
    private boolean isEnlarge() {
        boolean flag;
        if (isFull() == true) {
            T[] oldData = data;
            data = (T[]) (new Object[listSize + 1]);
            System.arraycopy(oldData, 0, data, 0, count);
            listSize++;
            flag = true;
        } else
            flag = false;
        return flag;
    }

    // 判斷數組是否已經存滿,這個是私有的方法。
    // 既然是動態的數組,對外肯定是不會滿的,所以設置爲私有的。
    private boolean isFull() {
        if (count == data.length)
            return true;
        else
            return false;
    }

    // 判斷動態數組是否爲空
    private boolean isEmpty() {
        if (count == 0)
            return true;
        else
            return false;
    }

    /**
     * 公開的方法
     */

    // 在指定位置添加
    public MyArrayList<T> add(int index, T t) {
        if (index > listSize) {
            throw new ArrayIndexOutOfBoundsException(index + ">" + listSize);
        }
        isEnlarge();// 檢查是否需要擴充內存
        // 在中間位置插入時候需要將後面的元素後移
        if (index + 1 <= count) {
            for (int i = count - 1; i >= index; i--)
                data[i + 1] = data[i];
        }
        // 插入元素
        data[index] = t;
        count++;
        return this;
    }

    // 在尾部添加
    public MyArrayList<T> add(T t) {
        add(count, t);
        return this;
    }

    // 刪除元素
    public MyArrayList<T> del(int index) throws Exception {
        if (isEmpty() == true)
            throw new Exception("數組爲空無法刪除...");
        else if (isEmpty() == false) {
            if (index <= count - 2) {
                for (int i = index; i <= count - 2; i++) {
                    data[i] = data[i + 1];
                }
                count--;
            } else if (index == count - 1) {
                count--;
            }
        }
        return this;
    }

    // 刪除最後一個元素
    public MyArrayList<T> del() throws Exception {
        return del(count - 1);
    }

    // 修改
    public MyArrayList<T> set(int index, T t) {
        data[index] = t;
        return this;
    }

    // 查詢
    public T get(int index) {
        if (index > count - 1 || index < 0) {
            System.out.println("下標錯誤...");
            return null;
        } else
            return data[index];
    }

    // 打印數組
    public void printList() {
        for (int i = 0; i < count; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
        System.out.println("長度是" + count);
    }

}

測試類:MainTest.java

public class MainTest {

    public static void main(String[] args) throws Exception {

        MyArrayList<String> mal = new MyArrayList<String>(3);
        mal.add("000").printList();
        mal.add(0, "111").printList();
        mal.add(1, "222").printList();
        mal.add("222").printList();
        mal.add("333").printList();
        mal.add("444").printList();
        mal.add("666").printList();
        mal.add(2, "YYY").printList();
        mal.del().printList();
        mal.del(2).printList();
        mal.del(0).printList();
        mal.add("999").printList();
        System.out.println(mal.get(1));
        mal.set(0, "9090").printList();
    }
}

測試結果:

000 
長度是1
111 000 
長度是2
111 222 000 
長度是3
111 222 000 222 
長度是4
111 222 000 222 333 
長度是5
111 222 000 222 333 444 
長度是6
111 222 000 222 333 444 666 
長度是7
111 222 YYY 000 222 333 444 666 
長度是8
111 222 YYY 000 222 333 444 
長度是7
111 222 000 222 333 444 
長度是6
222 000 222 333 444 
長度是5
222 000 222 333 444 999 
長度是6
000
9090 000 222 333 444 999 
長度是6
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章