回顾数据结构---数组

没啥可说的  直接上代码

package shuzu;

public class Array<E> {
    private E[] data;
    private int size;

    //构造函数  传入容量capacity
    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;
    }

    //在末尾添加新元素   时间复杂度:0(1)
    public void addLast(E e) {
        if (size == data.length)
            throw new IllegalArgumentException("AddLast failed.Array is full");
        data[size] = e;
        size++;
    }
  //  时间复杂度:0(n)
    public void addFirst(E e) {
        add(0, e);
    }

    //向指定位置插入新元素   时间复杂度:0(n/2)=O(n)
    public void add(int index, E value) {

        if (index < 0 || index > size)
            throw new IllegalArgumentException("Requeire index >= 0 and index <= size");
        if (index == data.length)
            resize(2*data.length);
        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = value;
        size++;
    }

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

    //获得索引对应的值  时间复杂度:O(1)
    public E get(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal");
        return data[index];
    }

    //设置索引为index 的值为 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 时间复杂度:O(n)
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i] == e)
                return true;
        }
        return false;
    }

    //查找数组中元素e所在的索引,如果不存在 返回-1   时间复杂度:O(n)
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return i;
        }
        return -1;
    }

    //删除下标为index的元素  将元素返回  时间复杂度:0(n/2)=O(n)
    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;
        //如果实际长度与总长度的1/2相同 进行缩容
        if(size == data.length/2)
            resize(1/2 * data.length);
        return ret;
    }
    //时间复杂度:0(n)
    public E removeFirst() {
        return remove(0);
    }
    //时间复杂度:0(1)
    public E removeLast() {
        return remove(size - 1);
    }

    //从数组中删除元素e    时间复杂度:0(1)
    public boolean removeElement(E e) {
        int index = find(e);
        if (index != -1) {
            remove(index);
            return true;
        }
        return false;
    }

    //动态数组   时间复杂度:0(n)
    private void resize(int s){
        E[] newData = (E[])new Object[s];
        for(int i = 0;i < size;i++){
            newData[i] = data[i];
        }
        data = newData;
    }
}

猜你喜欢

转载自blog.csdn.net/wanmingJKing/article/details/84027010