Java数据结构之动态数组

动态数组

  • 实现需求
    1. 增:(add(),addFirst(),addLast())
    2. 删:(remove(),removeFirst(),removeLast())
    3. 改:(set(),)
    4. 查 :(find(),contains(),isEmpty(),get(),getLast(),getFirst(),getCapacity(),getSize())
    5. 动态改变size:(reSize())
//代码实现
public class Array<E>{
    private E[] data;
//数组中包含的实际元素个数
    private int size;

/*构造函数*/
//有参构造函数,传入数组的容积
    public Array(int capacity){
        data = (E[])new Object[capacity];
        size = 0;
    }
//无参构造函数,默认容积为10
    public Array(){
        this(10);
    }
/******************************增********************************/
//增需要注意:

/*
1.每次增加size++
2.需呀进行初始判断index的取值范围
3.如果不足,需要扩容
*/

    public void add(int index,E e){
        //判断index的取值范围
        if(index < 0 || index > size){
            throw new IllegalArgumentException("AddLast failed.Require index>=0 and index <= size");
        }
        //如果数组容积已满,必须扩容
        if(size == arr.length){
            resize(2*data.length)
        }
        for(int i  = size-1; i>index;i--){
            data[i+1] = data[i];
        }
        data[index] = e;
        size++;
    }
    public void addFirst(E e){
        add(0,e);
    }
    public void addLast(E e){
        add(size,e);
    }
}

/****************************删***********************************/
//删需要注意:

/*
1.每次删除size--
2.需要进行初始判断index的取值范围
3.如果剩余容积过大,需要缩容
*/
    public E remove (int index){
        if(index < 0 || index > size){
                throw new IllegalArgumentException("remove failed.Require index>=0 and index <= size");
            }

        E ret = data[index];

        for(int i = index;i < size-1 ;i++){
            data[i] = data[i+1];
        }
        size--;
        data[size] = null;//为了加快gc的回收

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

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

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

    public E removeElement(E e){
        int index = find(e);
        if(index != -1){
        remove(index);
        }
    }
/**********************改*********************************/

    public void set(int index, E e){
        if(index < 0 || index > size){
                throw new IllegalArgumentException("set failed.Require index>=0 and index <= size");
            }
        data[index] = e;
    }
/*******************************查***************************/
/*
find()
contains()
isEmpty(),
get(),getLast(),getFirst(),
getCapacity()
getSize())
*/
    public int getCapacity(){
        return data.length;
    }
    public int getSize(){
        return size;
    }
    public int find(E e){
        for(int i =0;i<size;i++){
            if(e.equals(data[i])){
            return i;
            }
        }
        return -1;
    }
    public boolean contains(E e){
        for(int i =0;i<size;i++){
            if(e.equals(data[i])){
                return true;
            }
        }
        return false;
    }
    public boolean isEmpty(){
        if(size==0){
            return true;
        }
        return false;
    }
    public E get(int index){
        if(index < 0 || index > size){
                throw new IllegalArgumentException("get failed.Require index>=0 and index <= size");
        }
        return data[index];
    }
    public E getLast(int index){
        return get(size);
    }
    public E getFirst(int index){
        return get(0);
    }
/*******************扩容***************/
/*
    1.需要新建一个数组,将原数组中数据赋值过去
    2.修改原数组的引用,指向新数组
*/
    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();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41263632/article/details/81736216