From c698b43a6b623059d7d7afcd1f7378a96630d8dc Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 8 Oct 2019 22:41:49 +0800 Subject: [PATCH 1/2] toString --- DataStructures/Queues/Queues.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/DataStructures/Queues/Queues.java b/DataStructures/Queues/Queues.java index d5b9a8e5..5db41be4 100644 --- a/DataStructures/Queues/Queues.java +++ b/DataStructures/Queues/Queues.java @@ -130,6 +130,20 @@ class Queue { public int getSize() { return nItems; } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = front; ; i = ++i % maxSize) { + sb.append(queueArray[i]).append(", "); + if (i == rear) { + break; + } + } + sb.replace(sb.length() - 2, sb.length(), "]"); + return sb.toString(); + } } /** @@ -161,5 +175,6 @@ public class Queues { System.out.println(myQueue.peekFront()); // Will print 2 System.out.println(myQueue.peekRear()); // Will print 7 + System.out.println(myQueue.toString()); // Will print [2, 5, 3, 7] } } From 5b185e81918c4ec8a88fe7ed48c4812eff6cecdd Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Thu, 10 Oct 2019 14:06:38 +0800 Subject: [PATCH 2/2] Update Queues.java --- DataStructures/Queues/Queues.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStructures/Queues/Queues.java b/DataStructures/Queues/Queues.java index 5db41be4..2f820d70 100644 --- a/DataStructures/Queues/Queues.java +++ b/DataStructures/Queues/Queues.java @@ -157,7 +157,7 @@ public class Queues { * * @param args Command line arguments */ - public static void main(String args[]) { + public static void main(String[] args) { Queue myQueue = new Queue(4); myQueue.insert(10); myQueue.insert(2);