package com.my;

import java.util.Arrays;
/**
 * 顺序队列
 * @author wanjn
 *
 */
public class ArrayQueue {
    private int index = 0 ;//队尾指针
    private Object[] objs;
    public ArrayQueue(){
        objs = new Object[10];
    }
    //进队列
    public void add(Object value){
        if (index>=10) {
            throw new RuntimeException("长度越界");
        }
        objs[index++] = value;//尾指针前置法,index尾指针指向队尾元素的下一个位置
    }
    //出队列
    public Object poll(){
        Object oldValue = objs[0];
        objs[0] = null;
        Object[] newArry = new Object[10];
        System.arraycopy(objs, 1, newArry, 0, 9);
        objs = newArry;
        index--;
        return oldValue;
    }
    @Override
    public String toString() {
        return "ArrayQueue [index=" + index + ", objs=" + Arrays.toString(objs)
                + "]";
    }
    
}

 

 posted on 2018-01-26 16:47  改变一下  阅读(148)  评论(0编辑  收藏  举报