Skip to content

Commit 86d6a59

Browse files
authored
Merge pull request TheAlgorithms#995 from shellhub/master
toString
2 parents ed999c1 + 5b185e8 commit 86d6a59

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

DataStructures/Queues/Queues.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ public boolean isFull() {
130130
public int getSize() {
131131
return nItems;
132132
}
133+
134+
@Override
135+
public String toString() {
136+
StringBuilder sb = new StringBuilder();
137+
sb.append("[");
138+
for (int i = front; ; i = ++i % maxSize) {
139+
sb.append(queueArray[i]).append(", ");
140+
if (i == rear) {
141+
break;
142+
}
143+
}
144+
sb.replace(sb.length() - 2, sb.length(), "]");
145+
return sb.toString();
146+
}
133147
}
134148

135149
/**
@@ -143,7 +157,7 @@ public class Queues {
143157
*
144158
* @param args Command line arguments
145159
*/
146-
public static void main(String args[]) {
160+
public static void main(String[] args) {
147161
Queue myQueue = new Queue(4);
148162
myQueue.insert(10);
149163
myQueue.insert(2);
@@ -161,5 +175,6 @@ public static void main(String args[]) {
161175

162176
System.out.println(myQueue.peekFront()); // Will print 2
163177
System.out.println(myQueue.peekRear()); // Will print 7
178+
System.out.println(myQueue.toString()); // Will print [2, 5, 3, 7]
164179
}
165180
}

0 commit comments

Comments
 (0)