在 Java 中,我不知道如何使用新的比较器创建一个新的 PriorityQueue
但没有给定队列长度?如何创建它?
我知道我可以写:
Queue<Node> theQueue = new PriorityQueue<Node>(15,new Comparator<Node>();
但我希望队列能像LinkedList
那样工作,我的意思是它的长度不是固定的,我该如何声明呢?
最佳答案
现代答案,截至 2021 年:https://stackoverflow.com/a/30015986/139010
Java-8 之前的答案,供后人引用:
没有这样的构造函数。根据 JavaDocs,the default capacity is 11 ,因此您可以针对无参数 PriorityQueue
构造函数的类似行为指定它:
Queue<Node> theQueue = new PriorityQueue<Node>(11,new Comparator<Node>());
是的,the queue will grow if it needs to.
A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue, its capacity grows automatically. The details of the growth policy are not specified.x
关于java - 如何创建一个带有新比较器且没有指定初始容量的 PriorityQueue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15098731/