Java - Collections.binarySearch 与 PriorityQueue?
我可以使用 Collections.binarySearch() 方法来搜索 PriorityQueue 中的元素吗?否则,如何将搜索算法应用于 PriorityQueue?
我有这个(类 Evento 实现 Comparable):
public class PriorityQueueCAP extends PriorityQueue<Evento>{
// (...)
public void removeEventos(Evento evento){
Collections.binarySearch(this, evento); // ERROR!
}
}
我收到此错误:“集合类型中的方法 binarySearch(List>, T) 不适用于参数(PriorityQueueCAP,Evento)”
为什么?
提前致谢!
Can I use Collections.binarySearch() method to search elements in a PriorityQueue? Otherwise, how can I apply search algorithms to a PriorityQueue?
I have this (class Evento implements Comparable):
public class PriorityQueueCAP extends PriorityQueue<Evento>{
// (...)
public void removeEventos(Evento evento){
Collections.binarySearch(this, evento); // ERROR!
}
}
And I got this error: "The method binarySearch(List>, T) in the type Collections is not applicable for the arguments (PriorityQueueCAP, Evento)"
Why?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该将搜索算法应用于优先级队列。优先级队列旨在提供对集合中最高优先级元素的有效访问,仅此而已。
我知道这可能不是您所希望的答案;我见过太多因使用工具达到非预期目的而产生的问题,因此我觉得我应该提出警告。
You should not apply a search algorithm to a priority queue. A priority queue is designed to provide efficient access to the highest priority element in the collection, and that is all.
I know that this is probably not the answer for which you were hoping; I've seen too many problems arise from using tools for unintended purposes, and felt I should offer a word of warning.
此教程向您展示了如何启动收集以及可能的操作你可以在上面表演。你应该记住埃里克森所说的话。
This tutorial shows you how to initiate a collection and the possible operations you can perform on it. You should keep in mind what erickson said.