java自己实现的线性队列

来源:互联网 发布:淘宝汽车商城 编辑:程序博客网 时间:2024/06/05 11:44
public class MyQueue {
    Object[] obj;
    int size;
    
    public MyQueue() {
        this(10);
    }
    public MyQueue(int initLength) {
        obj = new Object[initLength];
    }
    
    public int size() {
        return size;
    }
    public Object[] expand(Object[] obj) {
        Object[] newObj = new Object[obj.length*2 + 1];
        for(int i=0; i
            newObj[i] = obj[i];
        }
        return newObj;
    }
    
    public boolean add(Object object) {
        if(size == obj.length) {
            obj = expand(obj);
        }
        obj[size] = object;
        size++;
        return true;
    }
    public boolean remove() {
        if(size == 0 ) {
            return false;
        }
        for(int i=0; i
            obj[i] = obj[i+1];
        }
        obj[size-1] = null;
        size--;
        return true;
    }
    
    public Object get(int index) {
        return obj[index];
    }
}
原创粉丝点击