Java Doc说:创建一个PriorityQueue,其默认初始容量(11)根据其自然顺序对其元素进行排序.
但是当我写这样的测试时:
public class Test {
public static void main (String a[]) {
Queue<Integer> queue = new PriorityQueue<Integer>();
for (int i = 1; i <= 20; i++) {
queue.offer(i);
}
System.out.println(queue);
Queue<Integer> queue2 = new PriorityQueue<Integer>();
for (int i = 20; i >= 1; i--) {
queue2.offer(i);
}
System.out.println(queue2);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[1, 2, 7, …Run Code Online (Sandbox Code Playgroud) java ×1