打印优先队列的内容[java]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22928255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:05:45  来源:igfitidea点击:

Print content of priority queue[java]

javaqueuepriority-queue

提问by ERJAN

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

如何使 print_queue 在 java 中正常工作?这是我自己的队列实现。

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

使用 Iterator() 工作正常,除了它以随机顺序打印数字。

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 dont' know how to traverse:

我尝试使用 toArray() 但它返回 Object[] ,我不知道如何遍历:

Object[] queue_object_array = x.toArray() ;
Arrays.sort(queue_object_array) ;

回答by user207421

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

使用 Iterator() 工作正常,除了它以随机顺序打印数字。

That's exactly what it says it will do in the Javadoc. The only way to get the ordering in the PriorityQueueis to use the poll()or remove()methods.

这正是它在 Javadoc 中所说的。在 中获得排序的唯一方法PriorityQueue是使用poll()orremove()方法。

回答by Rahul Arora

You can convert the Priority Queue object into an array object. You can then print this array object.

您可以将 Priority Queue 对象转换为数组对象。然后你可以打印这个数组对象。

Object[] arr = priorityQueue.toArray();

回答by hqt

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);