public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedHashSet<Integer> a=new LinkedHashSet<Integer>();
a.add(1);
a.add(5);
a.add(3);
a.add(6);
a.add(15);
a.add(9);
int b=11;
PriorityQueue<Integer> one=new PriorityQueue<Integer>(a);
System.out.print(one);
one.offer(2);
for(int i=0;i<n;i++)
{System.out.print(one.poll());
one.remove();
}
}
[1, 5, 3, 6, 15, 9]7
1
5
2
6
15
9
3
13615Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractQueue.remove(Unknown Source)
at PriorityqueueDemo.main(PriorityqueueDemo.java:30)
如果最后的for中不用poll()方法,用Queue中的peek()还是element()方法,就不会有exception
// TODO Auto-generated method stub
LinkedHashSet<Integer> a=new LinkedHashSet<Integer>();
a.add(1);
a.add(5);
a.add(3);
a.add(6);
a.add(15);
a.add(9);
int b=11;
PriorityQueue<Integer> one=new PriorityQueue<Integer>(a);
System.out.print(one);
one.offer(2);
for(int i=0;i<n;i++)
{System.out.print(one.poll());
one.remove();
}
}
[1, 5, 3, 6, 15, 9]7
1
5
2
6
15
9
3
13615Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractQueue.remove(Unknown Source)
at PriorityqueueDemo.main(PriorityqueueDemo.java:30)
如果最后的for中不用poll()方法,用Queue中的peek()还是element()方法,就不会有exception
解决方案 »
- 【高分请教】JAVA位移熟悉的兄弟进
- java中使用substance look&feel加水印,水印总是显示不出来
- 一道软件设计大赛的模拟试题,求解?
- 把数字变成汉字
- Java写的API可以封装成标准语言的吗?
- JFreeChart设置背景色时遇到的怪问题
- socket通讯,散分!
- 请feiyuegaoshan(飞跃) 来领分 (其他朋友别进)
- 如果给出一个人的出生日期如:2002-10-29,在得出系统日期:new Date(),请问怎样以最简单快捷的方法可以得出该人的岁数?
- 哪里有《最新 jbuilder 开发指南》一书下载?
- 如何才能正常运行含有中文的java程序?
- java内部类继承问题?
感觉是用element()方法,
但jdk中PriorityQueue的remove()和poll()的实现不一样,应该用poll不用remove,否则会违反队列的性质。
有空可以去看src研究下