toString 方法在 java 中向后打印

问题描述 投票:0回答:0

我创建了一个数组队列类,当对象被添加到队列时,它被添加到队列的末尾,当对象被移除时,队列中的第一个被移除。我添加了一个 toString 方法,该方法将打印出大小的字符串表示形式,然后是添加的值。我的其他方法工作正常,但 toString 方法打印顺序错误。这是我的完整代码,但我的问题出在我的 toString 方法上:


public class BoundedArrayQueue<E> implements BoundedQueue<E>{
    // fields
    private E[] storage;
    private int count;

    /**
     * Constructor
     * @param capacity
     */
    public BoundedArrayQueue(int capacity){
        if(capacity < 0){
            throw new IllegalArgumentException("Cannot have a negative queue length");
        }
        storage = (E[]) new Object[capacity];
        count = 0;
    }

    /**
     * Put a value on the end of the queue
     *
     * @param val The value to be added
     * @throws QueueOverFill when the stack is full
     */
    @Override
    public void add(E val) throws QueueOverFill {
        if (!hasSpace()){
            throw new QueueOverFill("Queue is full");
        }
        storage[count] = val;
        count++;
    }

    /**
     * Remove the first value from the queue
     *
     * @return The value removed from the queue
     * @throws QueueUnderEmpty when the stack is empty
     */
    @Override
    public E remove() throws QueueUnderEmpty {
        if(!hasValues()){
            throw new QueueUnderEmpty("Queue is empty");
        }
        E removed = storage[0];
        for(int i = 1; i < count; i++){
            storage[i - 1] = storage[i];
        }
        storage[count - 1] = null;
        count--;
        return removed;
    }

    /**
     * Retrieve the first value in the stack
     *
     * @return The first value in the queue
     * @throws QueueUnderEmpty when the stack is empty
     */
    @Override
    public E peek() throws QueueUnderEmpty {
        if(!hasValues()){
            throw new QueueUnderEmpty("Queue is empty");
        }
        return storage[0];
    }

    /**
     * Check the state of the queue
     *
     * @return True, when the queue has values
     */
    @Override
    public boolean hasValues() {
        return count > 0;
    }

    /**
     * Check the state of the queue
     *
     * @return True, when the queue has room for new values
     */
    @Override
    public boolean hasSpace() {
        return count != storage.length;
    }

    /**
     * Get the current size of the queue
     * (Debugging method)
     *
     * @return The number of elements currently in the queue
     */
    @Override
    public int getSize() {
        return count;
    }

    /**
     * Get the maximum size of the queue
     * (Debugging method)
     *
     * @return The maximum number of elements the queue can hold
     */
    @Override
    public int getCapacity() {
        return storage.length;
    }

    /**
     * Get a String representation of the queue
     * (Debug method)
     * @return The string representation
     */
    @Override
    public String toString(){
        if(!hasValues()) {
            String str = "[ " + getSize() + " : ]";
            return str;
        }
        String str = "[ " + getSize() + " : " + storage[count -1];
        for(int i = count - 2 ; i >= 0; i--) {
            str += ", " + storage[i];
        }
        str += " ]";
        return str;

    }

    public static void main(String[] args) throws QueueOverFill {
        BoundedArrayQueue<Integer> bas = new BoundedArrayQueue<Integer>(10);
        bas.add(4);
        bas.add(7);
        bas.add(8);
        System.out.println(bas.toString());
    }
}

这是我得到的输出: [ 3 : 8, 7, 4 ]

这是我要实现的输出: [ 3 : 4, 7, 8 ]

我做错了什么,我该如何改变?

java arrays data-structures tostring
© www.soinside.com 2019 - 2024. All rights reserved.