数据结构 Array 实现和使用。

Array 类

public class Array<E> {

    private E[] data;
    private int size;

    /**
     * 构造函数,传入数组容量capacity构造Array
     * @param capacity
     */
    public Array(int capacity) {
        data = (E[])new Object[capacity];
        size = 0;
    }

    /**
     * 无参构造函数,默认数组容量capacity=10
     */
    public Array() {
        this(10);
    }

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

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

    /**
     * 返回数组是否为空
     * @return boolean
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 向数组末尾添加新元素
     * @param e
     */
    public void addLast(E e) {

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

        add(size, e);
    }

    /**
     * 向数组起始位置添加新元素
     * @param e
     */
    public void addFirst(E e) {

        add(0, e);
    }

    /**
     * 往index插入新元素e
     * @param index
     * @param e
     */
    public void add(int index, E e) {

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

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

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

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

    /**
     * 获取index索引位置的元素
     * @param index
     * @return int
     */
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Get failed.Index is Illegal.");
        }

        return data[index];
    }

    public E getLast() {
        return get(size - 1);
    }

    public E getFirst() {
        return get(0);
    }

    /**
     * 修改index索引位置元素为e
     * @param index
     * @param e
     */
    public void set(int index, E e) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Get failed.Index is Illegal.");
        }

        data[index] = e;
    }

    /**
     * 查看数组中是否存在元素e
     * @param e
     * @return
     */
    public boolean contains(E e) {
        for (E item : data) {
            if (item == e) {
                return true;
            }
        }
        return false;
    }

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

    /**
     * 从数组中删除第一个元素,并返回删除的元素
     * @return
     */
    public E removeFirst() {
        return remove(0);
    }

    /**
     * 从数组中删除最后一个元素,并返回删除的元素
     * @return
     */
    public E removeLast() {
        return remove(size-1);
    }

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

    /**
     * 从数组中删除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;

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

    @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();
    }

    /**
     * 为数组重新分配 Capacity
     * @param newCapacity
     */
    private void resize(int newCapacity) {
        E[] newData = (E[])new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }
}

Array 类测试

public class Main {

    public static void main(String[] args) {

        Array<Integer> arr = new Array<>();
        for (int i = 0; i < 10; i ++) {
            arr.addLast(i);
        }
        System.out.println(arr);

        arr.add(1, 100);
        System.out.println(arr);

        arr.addFirst(-1);
        System.out.println(arr);

        arr.remove(2);
        System.out.println(arr);

        arr.removeElement(4);
        System.out.println(arr);

        arr.removeFirst();
        System.out.println(arr);
    }
}

运行结果

Array: size = 10, capacity = 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array: size = 11, capacity = 20
[0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array: size = 12, capacity = 20
[-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array: size = 11, capacity = 20
[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array: size = 10, capacity = 20
[-1, 0, 1, 2, 3, 5, 6, 7, 8, 9]
Array: size = 9, capacity = 20
[0, 1, 2, 3, 5, 6, 7, 8, 9]