实现一个支持动态扩容的数组

package cm.com.datastructure.array;

/**
 * 支持动态扩容的数组
 * 初始容量为8 每次扩容2倍
 *
 * @author MemorysLiu
 * @version 1.0.0
 * @date 2019-07-29 18:53
 */
public final class Array {

    private static final int init_size = 8; //设定初始大小

    private Object [] objectArray;  //存放值的数组

    private int size;


    public Array(){
        objectArray = new Object [init_size];
    }

    Array(int size){
        if (size <= 0){
            throw new RuntimeException("数组大小不能小于等于0");
        }
        if (size <= 8){
            size = init_size;
        }
        objectArray = new Object [size];
    }

    private void setSize(int size){
        if (size <= 0){
            throw new RuntimeException("数组大小不能小于等于0");
        }
        if (size < objectArray.length){
            throw new RuntimeException("数组大小不能小于原数组");
        }
        if (size == objectArray.length){
            return;
        }
        Object [] array = new Object[size];
        for (int i = 0; i < objectArray.length; i++) {
            Object o = objectArray[i];
            array[i] = o;
        }
        objectArray = array;
    }

    public T get(int index){
        if (index < 0 || index > size){
            throw new RuntimeException("数组角标越界");
        }
        return (T)objectArray[index];
    }

    public void add(T t){
        if (this.size > objectArray.length){
            throw new RuntimeException("出大问题了");
        }
        if (this.size == objectArray.length){
            setSize(2 * objectArray.length);
        }
        objectArray[size++] = t;
    }

}

 

你可能感兴趣的:(数据结构,数据结构与算法)