java中的key - 优先级队列

发布于 2024-12-10 06:05:20 字数 1126 浏览 0 评论 0原文

我正在尝试学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不一样的天空 2024-12-17 06:05:20

优先级是通过其Comparable 接口在Key 实现中实现的。

在您链接到的示例中,它们使用 String 元素,因此优先级是 StringComparable 实现。

例如,如果您想实现数字优先级,则可以使用您的类创建一个优先级队列,该队列将实现 Comparable,比较优先级字段的数字值。

The priority is implemented within the Key implementation via its Comparable interface.

In the example you link to, they use String elements, so the priority is String's implementation of Comparable.

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.

夜唯美灬不弃 2024-12-17 06:05:20

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

You don't need Key[], just Comparable[].

小傻瓜 2024-12-17 06:05:20

就像其他人所说的那样,关注可比较的界面。了解 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文