📚 数据结构基础:(1)自定义数组类

********## 自定义数组类UML图

完整代码

public class Array<E> {

    private E[] data;
    private int size;

    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    public Array() {
        this(10);
    }

    // 获取数组中的元素
    public int getSize() {
        return size;
    }

    // 获取数组的容量
    public int getCapacity() {
        return data.length;
    }

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

    // 向所有元素后添加一个新元素
    public void addLast(E elem) {

//        if (size == data.length)
//            throw new IllegalArgumentException("AddLast failed. Array is full");
//
//        data[size] = elem;
//        size++;
        add(elem, size);
    }

    // 在所有元素前添加一个元素
    public void addFirst(E elem) {
        add(elem, 0);
    }

    // 指定位置插入元素
    public void add(E elem, int index) {

        if (size == data.length) {
            // 扩容
            resize(2 * data.length);
        }

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

        // 给index处腾出位置
        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = elem;
        size++;
    }

    E get(int index) {

        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Illegal index.");
        return data[index];
    }

    // 修改对应索引处的值
    void set(E elem, int index) {

        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Illegal index.");
        data[index] = elem;
    }

    // 查找数组中是否有元素e
    public boolean contains(E elem) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(elem))
                return true;
        }
        return false;
    }

    // 查找数组中元素e的索引,不存在返回-1
    public int find(E elem) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(elem))
                return i;
        }
        return -1;
    }

    // 从数组中删除index处的元素,并返回删除的元素
    public E remove(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Illegal index.");

        // 记录下要删除的元素
        E val = data[index];
        for (int i = index + 1; i < size; i++) {
            data[i - 1] = data[i];
        }
        size--;

        if (size == data.length / 4 && data.length / 2 != 0) {
            resize(data.length / 2);
        }

        return val;
    }


    public E removeFirst() {
        return remove(0);
    }

    public E removeLast() {
        return remove(size - 1);
    }

    // 从数组中删除元素e
    public void removeElement(E elem) {
        int idx = find(elem);
        if (idx != -1) {
            remove(idx);
        }
    }

    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }


    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();
        res.append(String.format("Array size = %d, capacity = %d\n", size, data.length));
        res.append("[");
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1) {
                res.append(", ");
            }
        }
        res.append("]");

        return res.toString();
    }

}

posted on 2022-03-03 16:34  micromatrix  阅读(39)  评论(0编辑  收藏  举报

导航