简体   繁体   English

键入Java-优先级队列

[英]key in java - priority queue

I am trying to learn java and I am stumped by this example- I can't find what the key is 我正在尝试学习Java,但对此示例感到困惑-我找不到关键是什么

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. 这是优先级队列的示例..我从http://algs4.cs.princeton.edu/24pq/OrderedArrayMaxPQ.java.html获得它,我认为优先级队列应该具有数据+优先级值。 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++;
    }

The priority is implemented within the Key implementation via its Comparable interface. 优先级是通过其Comparable接口在Key实现中实现的。

In the example you link to, they use String elements, so the priority is String 's implementation of Comparable . 在您链接到的示例中,它们使用String元素,因此优先级是StringComparable实现。

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. 例如,如果要实现数字优先级,则可以使用您的类创建一个优先级队列,该队列将实现Comparable ,比较优先级字段的数字值。

您不需要Key[] ,只需Comparable[]

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. 了解Java如何实现优先级队列的一个好方法是注意String,Integer,Float和其他类实现Comparator接口的事实。

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. 1)玩弄简单的Java数据结构(例如Vectors和TreeMaps)的排序。 2) Look through the Collections API. 2)浏览Collections API。 Write a program which, for example, uses Collections.sort to sort some different lists. 编写一个程序,例如,使用Collections.sort对一些不同的列表进行排序。
3) Try to write an object which implements the Comparator interface, and sort a collection of these objects. 3)尝试编写一个实现Comparator接口的对象,并对这些对象的集合进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM