gpt4 book ai didi

java - 在 Java 中排序优先级队列

转载 作者:太空狗 更新时间:2023-10-29 22:49:17 25 4
gpt4 key购买 nike

<分区>

我试图在 PriorityQueue 中插入整数,我知道:

如果在构造PriorityQueue时没有指定比较器,则默认使用存储在队列中的数据类型的比较器。默认比较器将订购队列按升序排列

但是,我得到的输出没有按排序顺序排列。运行以下代码后的输出是:[2, 4, 8, 6]

public static void main(String args[]) {
PriorityQueue<Integer> q = new PriorityQueue<Integer>(10);
q.offer(4);
q.offer(2);
q.offer(8);
q.offer(6);

System.out.print(q);
}

谁能解释一下为什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com