自定义动态数组的实现

数组最大的优点:快速查询


动态数组:

public class Array<E> {   //E表示类型

    private E[] data;
    private int size;//数组中有效元素的个数,值指向数组中第一个没有值的位置

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

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

    //获取数组中有效元素个数的方法
    public int getSize(){
        return size;
    }

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

    //判断数组中是否有元素
    public boolean isEmpty(){
        return size == 0;
    }

    //size的值指向数组中第一个没有值的位置
    //所以向数组末尾添加元素,其实就等同于向size的值所在的位置添加元素
    //向所有元素的后添加一个新元素
    //O(1)
    //平均每次addLast操作,进行2次基本操作,这样均摊计算,时间复杂度是O(1)的
    public void addLast(E e){

//        if(size == data.length){
//            //则此时数组不能容纳一个新的元素,添加操作应该失败
//            throw new IllegalArgumentException("Array is full");
//        }
//
//        //下面两句等同于data[size++] = e;
//        data[size] = e;
//        size ++;

        //复用add方法
        add(size,e);
    }

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

    //向数组中指定位置添加元素
    //在index索引处插入一个新元素e
    //O(n/2),即O(n)
    public void add(int index, E e){
        //然后要保证用户传入的index是合法的
        if(index < 0 || index > size){
            throw new IllegalArgumentException("Required index >= 0 and index <= size");
        }

        //首先还是要保证数组有足够的空间来容纳要添加的元素
        //扩容
        if(size == data.length){
            //则此时数组不能容纳一个新的元素,扩容
            resize(2 * data.length);
        }

        for(int i = size - 1; i >= index; i--){
            //从最后一个元素的位置(size-1),到我们要插入的位置(index),
            //每一个元素我们都要先向后挪一个位置
            data[i + 1] = data[i];
        }

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

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

    //获取index索引位置的元素
    //O(1)
    public E get(int index){
        //对用户传进的索引进行判断,保证操作的索引合法
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("Index is Illegal");
        }
        return data[index];
    }

    //修改index索引位置的元素为e
    //O(1)
    public void set(int index, E e){
        //对用户传进的索引进行判断,保证操作的索引合法
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("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){
            if(data[i].equals(e)){
                return true;
            }
        }
        return false;
    }

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

    //从数组中删除index位置的元素,返回删除的元素
    //O(n/2) = O(n)
    public E remove(int index){
        //对用户传进的索引进行判断,保证操作的索引合法
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("Index is Illegal");
        }

        E ret = data[index];//把待删除的元素保存起来,作为返回值返回
        for(int i = index + 1; i < size; i++){
            //对于index之后的每一个元素,都让它向前挪一个位置
            data[i - 1] = data[i];
        }

        size--;
        data[size] = null;

        //缩容
        //后面的改进,采用Lazy策略来防止复杂度的震荡
        //if(size == data.length / 2){
        if(size == data.length / 2 && data.length / 2 != 0){
            resize(data.length / 2);
        }
        return ret;
    }

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

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

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

    //改变数组容量大小
    //O(n)
    private void resize(int newCapacity){
        E[] newData = (E[])new Object[newCapacity];
        for(int i = 0; i < size; i++){
            newData[i] = data[i];
        }
        data = newData;
    }
}

测试类1:测试基本功能

public class Main1 {
    public static void main(String[] args) {
        Array arr = new Array(20);

        for(int i = 0; i < 10; i++){
            arr.addLast(i);
        }

        /*
            Array: size = 10 , capacity = 20
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        arr.add(1,100);
        /*
            Array: size = 11 , capacity = 20
            [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        /*
            Array: size = 12 , capacity = 20
            [-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        arr.addFirst(-1);
        System.out.println(arr);

        arr.remove(2);
        /*
            Array: size = 11 , capacity = 20
            [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        arr.removeElement(4);
        /*
            Array: size = 10 , capacity = 20
            [-1, 0, 1, 2, 3, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);


        arr.removeFirst();
        /*
            Array: size = 9 , capacity = 20
            [0, 1, 2, 3, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);
    }
}


测试类2:测试扩容功能

public class Main {
    //测试扩容功能
    public static void main(String[] args) {
        Array arr = new Array();

        for(int i = 0; i < 10; i++){
            arr.addLast(i);
        }

        /*
            Array: size = 10 , capacity = 20
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        arr.add(1,100);
        /*
            Array: size = 11 , capacity = 20
            [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        /*
            容量由默认的10扩容成了20
            Array: size = 11 , capacity = 20
            [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        arr.addFirst(-1);
        System.out.println(arr);

        //arr.remove(2);
        /*
            Array: size = 11 , capacity = 20
            [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        //System.out.println(arr);

        //arr.removeElement(4);
        /*
            Array: size = 10 , capacity = 20
            [-1, 0, 1, 2, 3, 5, 6, 7, 8, 9]
         */
        //System.out.println(arr);


        //arr.removeFirst();
        /*
            Array: size = 9 , capacity = 20
            [0, 1, 2, 3, 5, 6, 7, 8, 9]
         */
        //System.out.println(arr);
    }
}

猜你喜欢

转载自blog.csdn.net/hoji_James/article/details/80600946