title: Print content of priority queue
body: <p>How do I make the print_queue work properly in Java? This is my own implementation of a queue.</p>
  <p><strong>Using <code>Iterator()</code> works fine, except it prints numbers in random order.</strong></p>
  <pre><code>package data_structures_java ;
  import java.util.Iterator;
  import java.util.PriorityQueue ;
  import java.util.* ;
  public class Queue_implementation {
   
      PriorityQueue&lt;Integer&gt; actual_queue ;
      
      public Queue_implementation(){
          actual_queue = new PriorityQueue&lt;Integer&gt;() ;
          
      }
      
      public  void add(int num){
          actual_queue.add(num) ;
      }
      
      public int remove(){
            return actual_queue.remove() ;          
      }
      
      public int peek(){
          if( actual_queue.isEmpty()) return -1 ;
          else return actual_queue.peek() ;
      }
      
      public int element(){
          return actual_queue.element() ;
      }
      
      public void print_queue(){      
          PriorityQueue&lt;Integer&gt;copy = new PriorityQueue&lt;Integer&gt;();
          copy.addAll(actual_queue) ;        
          Iterator&lt;Integer&gt; through = actual_queue.iterator() ;
          while(through.hasNext() ) {
                  System.out.print(through.next() + &quot; &quot;) ;
          }
          System.out.println() ;
                  
          actual_queue.addAll(copy) ;
          
      }
      public static void main(String[] args) {            
          Queue_implementation x = new Queue_implementation() ;
          x.add(10) ;
          x.add(9) ;
          x.add(8) ;
          x.add(7) ;
          x.add(6) ;
          x.print_queue() ;
      }
  
  }
  </code></pre>
  <p>I tried to use <code>toArray()</code> but it returns <code>Object[]</code>, which I don't know how to traverse:</p>
  <pre><code>Object[] queue_object_array = x.toArray() ;
  Arrays.sort(queue_object_array) ;
  </code></pre>