8

How do I make the print_queue work properly in Java? This is my own implementation of a queue.

Using Iterator() works fine, except it prints numbers in random order.

package data_structures_java ;
import java.util.Iterator;
import java.util.PriorityQueue ;
import java.util.* ;
public class Queue_implementation {
 
    PriorityQueue<Integer> actual_queue ;
    
    public Queue_implementation(){
        actual_queue = new PriorityQueue<Integer>() ;
        
    }
    
    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<Integer>copy = new PriorityQueue<Integer>();
        copy.addAll(actual_queue) ;        
        Iterator<Integer> through = actual_queue.iterator() ;
        while(through.hasNext() ) {
                System.out.print(through.next() + " ") ;
        }
        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() ;
    }

}

I tried to use toArray() but it returns Object[], which I don't know how to traverse:

Object[] queue_object_array = x.toArray() ;
Arrays.sort(queue_object_array) ;
ERJAN
  • 23,696
  • 23
  • 72
  • 146

3 Answers3

10

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
8

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);
hqt
  • 29,632
  • 51
  • 171
  • 250
  • 1
    what is the difference between the code and `System.out.println(priorityQueue)` – ZhaoGang Apr 15 '20 at 14:48
  • @ZhaoGang thanks for the note. Actually it is the same after I checked. It just because my thinking process causes me to do so :D You can add a new answer, or I will edit my answer. – hqt Apr 15 '20 at 16:18
  • 1
    They are much the same, so no need for a new answer I think, please edit your answer if you would like to, thx. – ZhaoGang Apr 16 '20 at 01:32
  • 1
    @user207421 I don't say this method will print in order. It's useful for debugging. I am lazy to check but I remember the original post just wants to print out the content. – hqt Feb 07 '21 at 03:21
  • This does not print the PQ in priority order. Please read the question. Printing in order is what the question is about. *Ergo* you haven't answered it. Being too lazy to check is no excuse. I did. – user207421 Aug 20 '23 at 22:56
1

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());
Tushar
  • 528
  • 4
  • 20