Java队列(数组实现)

来源:互联网 发布:免费虚拟手机号软件 编辑:程序博客网 时间:2024/06/09 15:34
public class ArrayQueue{    private static final String TAG = "ArrayQueue";    private Object[] queue;    private int SIZE = 10;//初始化大小    private int front = 0;    private int rear = 0;    private int usesize = 0;    public ArrayQueue()    {        queue = new Object[SIZE];    }    public boolean isEmpty()    {        return (rear - front) == 0;    }    public int getSize()    {        return rear - front;    }    public void insert(Object obj) throws Exception    {        if (front > rear || rear > SIZE) throw new Exception("无法进入队列!");        if (((rear - front) + 1) % SIZE == 0) throw new Exception("队列已满!");        queue[rear] = obj;        rear++;        usesize++;    }    public void delete() throws Exception    {        if (front >= rear || rear > SIZE) throw new Exception("无法删除!");        queue[front] = null;        front++;        usesize--;    }    public void display() throws Exception    {        if (usesize == 0) throw new Exception("空队列!");        for (int i = front; i < rear; i++)        {            System.out.print(queue[i] + "<-");        }        System.out.println("");    }    public static void main(String[] args) throws Exception    {        ArrayQueue aq = new ArrayQueue();        aq.insert("123");        aq.insert("qwe");        aq.insert("tt");        aq.insert("h8h0-----=1ew");        System.out.println(aq.queue[0]);        System.out.println(aq.queue[1]);        System.out.println(aq.queue[2]);        System.out.println(aq.queue[3]);        System.out.println(aq.getSize());        aq.display();    }} 
0 0