我知道 PriorityQueue 是一个sorted 的集合 集合元素 是有顺序的
类似的还有 Treeset 等 我查阅了API toString() 方法 上面写的
返回此 collection 的字符串表示形式。该字符串表示形式由 collection 元素的列表组成,这些元素按其迭代器返回的顺序排列,并用方括号 ("[]") 括起来。相邻元素由字符 ", "(逗号加空格)分隔。
所以我的理解是 应该按照 迭代的顺序 返回 应该是[-3,0,6,9]
但是却出现了意外的结果 [-3,0,9,6] 。
后来网上也有人问类似的问题 但是始终没有解决
同学也都不会
希望CSDN的高手 帮我看一下
PriorityQueue a=new PriorityQueue();
a.offer(6);
a.offer(-3);
a.offer(9);
a.offer(0);
System.out.println(a);//这里为什么是 输出[-3,0,9,6}呢
解决方案 »
- 请教一条题目
- jComboBox怎么设置为不可用?
- 正则表达式做词法分析
- 继承问题~想了两天了也没法解决,请教高手
- java中仅由{ }包裹的代码块的执行原理
- 如果我要在一个本地java main()程序里面用到sql server的jdbc驱动程序来操作SqlServer数据库,那么要把那三个sql server的jdbc驱动文件放
- 小问题,过来看,可以的话把20拿走:)
- 关于jdbc异常:java.sql.SQLException: No data found 郁闷
- 刚下载了j2sdkee1.2.1 谁能告诉我个大概这家伙怎么用呀,谢谢。另问,要进行java的开发是不是有这个就可以了。
- java大虾救命啊!!毕业不了啦,!!毕业设计要用java做一个面向对象的产品数据管理系统,(pdm),可我一不会java,二不会visualcafe,怎么办啊!?帮帮我啊!!
- java求助。。。大侠帮忙找找错误
- 为什么点击"查询"会出现空指针错误
但是PriorityQueue的iterator返回的顺序是不定的The string representation consists of a list of the collection's elements in the
order they are returned by its iterator.This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).
谢谢大神 但是我就费解了 他这个 sorted 的意义 体现在哪里呢
既然是随机顺序
难不成 在集合内部的保存 顺序是有序的 但是输出 迭代 就变成了无序的了
此队列按照在构造时所指定的顺序对元素排序,既可以根据元素的自然顺序来指定排序(参阅 Comparable)PriorityQueue a = new PriorityQueue();
a.offer(6);
a.offer(-3);
a.offer(9);
a.offer(0);
System.out.println(a); while (!a.isEmpty()) {
System.out.print(a.poll() + ",");
}
public E next() {
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
if (cursor < size)
return (E) queue[lastRet = cursor++];
if (forgetMeNot != null) {
lastRet = -1;
lastRetElt = forgetMeNot.poll();
if (lastRetElt != null)
return lastRetElt;
}
throw new NoSuchElementException();
}