java中的key - 优先级队列
我正在尝试学习java,但我被这个例子难住了-我找不到关键是什么
public class OrderedArrayMaxPQ<Key extends Comparable<Key>> {
private Key[] pq; // elements
这是优先级队列的例子..我从http://algs4.cs.princeton.edu/24pq/OrderedArrayMaxPQ.java.html 我认为优先级队列应该有一个数据+一个优先级值。我不确定这些人是如何做到的。
public class OrderedArrayMaxPQ<Key extends Comparable<Key>> {
private Key[] pq; // elements
private int N; // number of elements
// set inititial size of heap to hold size elements
public OrderedArrayMaxPQ(int capacity) {
pq = (Key[]) (new Comparable[capacity]);
N = 0;
}
public boolean isEmpty() { return N == 0; }
public int size() { return N; }
public Key delMax() { return pq[--N]; }
public void insert(Key key) {
int i = N-1;
while (i >= 0 && less(key, pq[i])) {
pq[i+1] = pq[i];
i--;
}
pq[i+1] = key;
N++;
}
I am trying to learn java and I am stumped by this example- I can't find what the key is
public class OrderedArrayMaxPQ<Key extends Comparable<Key>> {
private Key[] pq; // elements
This is the example of the priority queue .. i got it from http://algs4.cs.princeton.edu/24pq/OrderedArrayMaxPQ.java.html
I thought a priority queue should have a data + a priority value. I am not sure how these guys are doing it.
public class OrderedArrayMaxPQ<Key extends Comparable<Key>> {
private Key[] pq; // elements
private int N; // number of elements
// set inititial size of heap to hold size elements
public OrderedArrayMaxPQ(int capacity) {
pq = (Key[]) (new Comparable[capacity]);
N = 0;
}
public boolean isEmpty() { return N == 0; }
public int size() { return N; }
public Key delMax() { return pq[--N]; }
public void insert(Key key) {
int i = N-1;
while (i >= 0 && less(key, pq[i])) {
pq[i+1] = pq[i];
i--;
}
pq[i+1] = key;
N++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
优先级是通过其
Comparable
接口在Key
实现中实现的。在您链接到的示例中,它们使用
String
元素,因此优先级是String
的Comparable
实现。例如,如果您想实现数字优先级,则可以使用您的类创建一个优先级队列,该队列将实现 Comparable,比较优先级字段的数字值。
The priority is implemented within the
Key
implementation via itsComparable
interface.In the example you link to, they use
String
elements, so the priority isString
's implementation ofComparable
.If you wanted to implement, say, a numeric priority, you'd create a priority queue with your class, which would implement
Comparable
, comparing the numeric value of the priority field.您不需要
Key[]
,只需Comparable[]
。You don't need
Key[]
, justComparable[]
.就像其他人所说的那样,关注可比较的界面。了解 java 如何实现优先级队列的一个好方法是注意 String、Integer、Float 和其他类实现 Comparator 接口这一事实。
这意味着,根据对象定义,它与相同类型的其他对象具有可比性(<、> 或 =)。
我建议,您不要学习优先级队列,而是
1) 尝试对更简单的 Java 数据结构(如向量和 TreeMap)进行排序。
2)查看Collections API。编写一个程序,例如使用 Collections.sort 对一些不同的列表进行排序。
3)尝试编写一个实现Comparator接口的对象,并对这些对象的集合进行排序。
Like the others say, focus on the comparable interface. A good way to get a feel for how java implements priority queues is to note the fact that String, Integer, Float, and other classes implement the Comparator interface.
This means that , by the objects definition, it is comparable (<,>, or =) to other objects of the same type.
I would suggest , rather than learning about priority queues, you
1) play with the sorting of simpler java data structures like Vectors and TreeMaps.
2) Look through the Collections API. Write a program which, for example, uses Collections.sort to sort some different lists.
3) Try to write an object which implements the Comparator interface, and sort a collection of these objects.