Print content of priority queue
Solution 1:
Using Iterator() works fine, except it prints numbers in random order.
That's exactly what it says it will do in the Javadoc. The only way to get the ordering in the PriorityQueue
is to use the poll()
or remove()
methods.
Solution 2:
One line solution: it is useful when you need to fast debug.
System.out.println(Arrays.toString(priorityQueue.toArray()));
Edit: Another concise code:
System.out.println(priorityQueue);
Solution 3:
If you need to print content of priorityQueue where each element is a 2D array you can try using:
System.out.println(Arrays.deepToString(priorityQueue.toArray());
If it's a 1D array:
System.out.println(Arrays.toString(priorityQueue.toArray());