日期:2014-05-20  浏览次数:21054 次

队列的tostring方法
Java code
package sun.way;


class QueueT {
    private int maxSize;
    private int[] queArr;
    private int front;
    private int rear;
    private int nItems;
    
    public QueueT(int s) {
        maxSize = s;
        queArr = new int[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;
    }
    
    public void insert(int j) {
        if (isFull())
            return;
        if (rear == maxSize - 1)
            rear = -1;
        queArr[++rear] = j;
        nItems++;
    }
    
    public int remove() {
        int temp = queArr[front++];
        if (front == maxSize)
            front = 0;
        nItems--;
        return temp;
    }
    
    public int peekFront(){
        return queArr[front];
    }
    
    public boolean isEmpty() {
        return (nItems == 0);
    }
    
    public boolean isFull() {
        return (nItems == maxSize);
    }
    
    public int size() {
        return nItems;
    }
    public String toString() {
        StringBuffer sb = new StringBuffer();
        for (int i = rear; i >=0; i--) {
            sb.append("" + queArr[i] + " ");
        }
        return sb.toString();
    }
}

public class QueueApp {
    public static void main(String[] args) {
        QueueT theQueue = new QueueT(10);
        theQueue.insert(39);
        theQueue.insert(23);
        theQueue.insert(12);
        theQueue.insert(54);
        theQueue.insert(63);
        theQueue.insert(36);
        
        System.out.print(theQueue);
        System.out.println();
        if (!theQueue.isEmpty())
            System.out.println("删除" + theQueue.remove());
        System.out.println("------------------");
        System.out.print(theQueue);        
    }
}



这个队列的tostring方法那里错了...为什么删除一个后打印还是一样的?
有什么办法没.帮忙改下啊

------解决方案--------------------
for (int i = rear;
这个rear 我怎么在remove里面没看到调整啊?
------解决方案--------------------
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = rear; i >=front; i--) {
sb.append("" + queArr[i] + " ");
}
return sb.toString();
}