數組簡單源碼

數組簡單源碼實現,做一個筆記。

package arrays.init;

/**
 * 數組初始化
 */
public class MyArrays {
    /**
     * 定義一個數組
     */
    private int[] intArrays;

    /**
     * 定義數組的實際長度
     */
    private int elems;

    /**
     * 定義數組的最大長度
     */
    private int length;

    public MyArrays() {
        elems = 0;
        length = 10;
        intArrays = new int[length];
    }

    public MyArrays(int length) {
        elems = 0;
        this.length = length;
        intArrays = new int[length];
    }

    /**
     * 獲取數組大小
     */
    public int getSize() {
        return elems;
    }

    /**
     * 遍歷數組
     */
    public void display() {
        for (int i = 0; i < elems; i++) {
            System.out.print(intArrays[i] + ",");
        }
        System.out.println();
    }

    /**
     * 添加元素
     */
    public boolean add(int value) {
        if (length < elems) {
            throw new IndexOutOfBoundsException("數組越界");
        } else {
            intArrays[elems] = value;
            elems++;
        }
        return true;
    }

    /**
     * 查找元素 根據元素值
     */
    public int find(int value) {
        int i = 0;
        for (i = 0; i < elems; i++) {
            if (intArrays[i] == value) {
                break;
            }
        }
        // not find
        if (i == elems) {
            return -1;
        }
        return i;
    }

    /**
     * 查找元素 根據下標
     */
    public int get(int i) {
        if (i < 0 || i >= elems) {
            throw new IndexOutOfBoundsException("數組越界");
        }
        return intArrays[i];
    }

    /**
     * 刪除元素
     */
    public boolean delete(int value) {
        int k = find(value);
        // not find
        if (k == -1) {
            System.out.println("元素:" + value + "不存在");
            return false;
        } else {
            if (k == elems - 1) { // 最後一個元素
                elems--;
            } else { // k 後面的元素向前移動
                for (int i = k; i < elems - 1; i++) {
                    intArrays[i] = intArrays[i + 1];
                }
                elems--;
            }
        }
        return true;
    }

    /**
     * 修改數組元素
     */
    public boolean modify(int oldValue, int newValue) {
        int k = find(oldValue);
        if (k == -1) {
            System.out.println("元素:" + oldValue + "不存在");
            return false;
        } else {
            intArrays[k] = newValue;
        }
        return true;
    }
}

測試類

package arrays;

import arrays.init.MyArrays;

public class Test {
    public static void main(String[] args) {
        // 1. 實例化數組
        MyArrays myArrays = new MyArrays(10);
        // 2. 獲取數組的大小
        System.out.println("數組大小:" + myArrays.getSize());
        // 3. 添加元素
        System.out.println("添加元素:" + myArrays.add(2));
        System.out.println("添加元素:" + myArrays.add(3));
        // 4. 遍歷數組
        myArrays.display();
        // 5. 根據下標查詢元素
        System.out.println("獲取元素:" + myArrays.get(0));
        // 6. 查找元素根據值
        System.out.println("獲取元素下標:" + myArrays.find(2));
        // 7. 刪除數組元素 根據元素值
        System.out.println("刪除元素:" + myArrays.delete(2));
        myArrays.display();
        // 8. 修改元素 oldValue newValue
        myArrays.modify(3, 4);
        myArrays.display();

    }
}

結果

數組大小:0
添加元素:true
添加元素:true
2,3,
獲取元素:2
獲取元素下標:0
刪除元素:true
3,
4,

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章