我有一個ArrayQueue
我正在爲java語言中的類實現。在java中實現ArrayQueue需要toString方法
我將交易對象存儲在ArrayQueue
中,我陷入了display()
方法(也就是我自己的toString()
)。但它只是返回參考。
這裏是我的方法:
//display the elements in the current queue
public String display() {
String result = "";
if(isEmpty()) {
throw new EmptyQueueException("Queue is empty.");
} else {
for (int i = 0; i < count; i++) {
result += "[" + Q[(front + i) % capacity] + "] ";
}
}
return result;
}
這是否意味着我需要一個toString()
方法爲我的對象,並調用它像:System.out.println(arrayqueue.display().toString())
?