我想显示ArrayQueue <Order>类型的订单列表
Order具有ArrayStack<String>作为其属性之一。我覆盖了toString()类中的Order方法,但是如何在ArrayStack类中覆盖它呢?因为这是我在显示时得到的输出:

订单编号名称日期 ArrayStack @ 481adc30

我要怎么做才能正确显示ArrayStack中的字符串?我是否要对ArrayStack类进行更改或在Display方法中进行某些更改?

这是我的显示方法:

 public void display(){
    if (!isEmpty())
    for (int i = 0; i < numberOfEntries; i++) {
        System.out.println(queue[(frontIndex + i) % queue.length]);
    }
    else System.out.println("You don't have any orders");
    }

ArrayStack类:
 public class ArrayStack < T > implements StackInterface < T >
{
    private T [] stack; // array of stack entries

    private int topIndex; // index of top entry

    private static final int DEFAULT_INITIAL_CAPACITY = 50;

    public ArrayStack ()
    {
        this (DEFAULT_INITIAL_CAPACITY);
    } // end default constructor


    public ArrayStack (int initialCapacity)
    {
        // the cast is safe because the new array contains null entries
        @ SuppressWarnings ("unchecked")
            T [] tempStack = (T []) new Object [initialCapacity];
        stack = tempStack;
        topIndex = -1;
    } // end constructor

    /*  Implementations of the stack operations */

订单类别:
   import java.util.Date;


public class Order {

    int orderNumber;
    String customerName;
    Date date;
    StackInterface <String> items;

Order( int number, String name, Date datum, StackInterface<String> item){
    orderNumber = number;
    customerName= name;
    date= datum;
    items = item;
}

/Overriding toString() to Display a list of Orders as one String line.
public String toString(){
    return orderNumber + " " + customerName + " " + date + " " + items;
}

最佳答案

您可以使用override toString()中的ArrayStack方法,如 here 所示。这样可以解决您的问题。

public String toString() {
    String result = "";

    for (int scan = 0; scan < top; scan++)
        result = result + stack[scan].toString() + "\n";

    return result;
}

10-07 18:43