Java數據結構-數組解析及類封裝自定義數組實現

概念:

數組是Java數據結構中最基本的數據,是存儲 一組長度固定同數據類型的集合。

優點:
  • 插入快:對於無序數組,只需要在數組末尾增加數據即可。但對於有序數組,需要查找到固定的位置,再插入數據,相對無序數組
  • 結構簡單
缺點:
  • 根據元素值查找慢:如果根據下標查找會比較快,但是根據元素值查找對於無序數組需要從第一個元素開始遍歷進行查找知道查找到所需要的數據。如果是有序數組可以通過合適的排序算法加快查找速度。
  • 刪除慢:除了需要遍歷查找到需要刪除的數據外,刪除數據後,後面的數據還需要向前移動一個位置,這也需要耗費較多時間。
  • 長度固定:不能動態擴容,如果一開始初始化的長度太大用不着浪費空間,如果一開始初始化的長度太小又裝不下,不夠靈活擴展性差。
類封裝實現簡單的數組結構
/**
 * 自定義類封裝實現數組,實現基本功能:
 * 增加數據
 * 獲取數據
 * 刪除數據
 * 查找數據
 * 修改數據
 */

public class MyArrayList {

    private Object[] mArray;  //定義數組,可以存儲任意數據類型 object

    private int size;//數組長度

    //無參構造,可以默認 大小
    public MyArrayList() {
        mArray = new Object[10];  //默認初始化 10 大小

    }

    //指定初始化大小
    public MyArrayList(int size) {
        mArray = new Object[size];
    }

    public int getSize() {
        if (mArray == null) {
            throw new NullPointerException("數組不能爲空");
        }
        return size;
    }

    /**
     * 增加數據
     *
     * @param o
     */
    public void add(Object o) {
        mArray[size] = o;    //將數據增加到最後  ,size初始值爲0 ,從0開始增加
        size++;

        //是否需要擴容
        if (size >= mArray.length) {
            //增加的數據 大於初始化的數組長度,裝不下了自動擴容
            int newCapacity = size * 2;    //擴容2倍--類似hashmap 實現原理
            //將原來數組拷貝到新的數組
            Object[] newArray = new Object[newCapacity];
            for (int i = 0; i < mArray.length; i++) {
                newArray[i] = mArray[i];
            }

            mArray = newArray;
        }

    }

    /**
     * 在固定的位置增加數據
     *
     * @param index
     * @param o
     */
    public void addWithIndex(int index, Object o) {
        isVaildIndex(index,"數組越界");
        size++;
        for (int i = index; i < size; i++) {
            mArray[i+1] = mArray[i];
        }
        mArray[index] = o;
    }

    /**
     * 根據下標查找數據
     *
     * @param index
     * @return
     */
    public Object get(int index) {
        isVaildIndex(index, "數組越界:不能獲取數組外的數據");
        return mArray[index];
    }


    /**
     * 根據下標刪除數據,將最後一個數據移動到刪除的數據的位置,然後
     *
     * @param index
     */
    public void delete(int index) {
        isVaildIndex(index, "數組越界:不能刪除數組外的數據");

        mArray[index] = mArray[size - 1];
        size--;

        Object[] newArray = new Object[size];
        for (int i = 0; i < size; i++) {
            newArray[i] = mArray[i];
        }

        mArray = newArray;

    }

    /**
     * 根據下標刪除數據,將需要刪除的數據之後的數據往前移
     *
     * @param index
     */
    public void deleteWithIndex(int index) {
        isVaildIndex(index,"數組越界,不能刪除數組長度外的數據 2 ");
        if (index < size-1){
            for (int i = 0; i < size-1; i++) {
                mArray[i+index] = mArray[i+index+1];
//                System.out.print("index1 :"+ mArray[i+index]);
            }
        }

        size--;
        //構建新的數組
        Object[] newArray = new Object[size];
        for (int i = 0; i < size; i++) {
//            System.out.print("index:"+ mArray[i]);
            newArray[i] = mArray[i];
        }
        mArray  = newArray;
    }

    /**
     * 修改元素
     * @param oldObj
     * @param newObj
     */
    public void find(Object oldObj,Object newObj){
        for (int i = 0; i < size; i++) {
            if (mArray[i] == oldObj){
                mArray[i] = newObj;
            }
        }
    }

    /**
     * 刪除某一區間數據
     * @param start
     * @param end
     */
    public void deleteRange(int start,int end){
        if (start<0 || start>end || end>=size){
            throw new ArrayIndexOutOfBoundsException("數組下標不一致");
        }

        for (int i = 0; i < size; i++) {
            if (i+end >size){
                mArray[i+start] = null;
            }else {
                mArray[i+start] = mArray[i+end];
            }
        }

        size -=end-start;
        Object[] newArray = new Object[size];
        for (int i = 0; i < size; i++) {
            newArray[i]  = mArray[i];
        }
        mArray = newArray;
    }

    /**
     * 若在一個條件語句中拋出異常,則程序能被編譯,但後面的語句不會被執行。
     * @param index
     * @param message
     */
    private void isVaildIndex(int index, String message) {
        if (index < 0 || index > size) {
            throw new ArrayIndexOutOfBoundsException(message);
        }
    }
}


//Test
public class Test {

    public static void main(String[] agrs){

        MyArrayList myArrayList = new MyArrayList();  //默認初始化10

        int size = myArrayList.getSize();

//        System.out.println("長度是:"+size); //0

        //添加數據
        myArrayList.add(3);
        myArrayList.add(5);

        System.out.println("長度是:"+myArrayList.getSize()); //2

//        display(myArrayList);  //3 5

        //添加固定位置的數據
        myArrayList.addWithIndex(1,15);

//        display(myArrayList); //3 15 5

        //刪除數據
        myArrayList.delete(0);

//        display(myArrayList); //5 15

//        System.out.println(myArrayList.getSize());
        myArrayList.deleteWithIndex(1);

//        display(myArrayList);

        myArrayList.find(5,8);
        display(myArrayList);

    }

    private static void display(MyArrayList myArrayList) {
        for (int i = 0; i <myArrayList.getSize(); i++) {
            System.out.println("元素是:"+myArrayList.get(i));
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章