我可以使用 Collections.binarySearch() 方法来搜索 PriorityQueue 中的元素吗?否则,如何将搜索算法应用于 PriorityQueue?
我有这个(Evento 类实现 Comparable):
public class PriorityQueueCAP extends PriorityQueue<Evento>{
// (...)
public void removeEventos(Evento evento){
Collections.binarySearch(this, evento); // ERROR!
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了这个错误:“类型集合中的方法 binarySearch(List>, T) 不适用于参数(PriorityQueueCAP,Evento)”
为什么?
提前致谢!
我试图在Java中使用PriorityQueue.
我有一些自定义对象,我以这种方式将它们添加到队列中:
Pet pet1 = new Pet();
Pet pet2 = new Pet();
Pet pet3 = new Pet();
PriorityQueue<Pet> queue = new PriorityQueue<Pet>();
queue.offer(pet1);
queue.offer(pet2);
queue.offer(pet3);
Run Code Online (Sandbox Code Playgroud)
此时,我意识到我的Pet对象必须实现Comparable才能从PriorityQueue获取ClassCastException.所以我让我的Pet实现Comparable,然后只返回0覆盖copmareTo(obj)方法.
但奇怪的是这里.当我...
queue.poll(); //return: pet1 queue: pet3, pet2
queue.poll(); //return: pet3 queue: pet2
Run Code Online (Sandbox Code Playgroud)
因为我按照pet1,pet2和pet3的顺序添加了它们,为什么在我第一次调用poll()时,它会对我的宠物序列进行排序?然后整个事情变得不再是一个队列,因为它的输入序列没有被保留,不是吗?
我怀疑它可能与Comparable接口和compareTo(obj)方法有关.但我所需要的只是保持其输入序列,所以我真的不需要比较任何东西或任何东西.
如何在队列中维护其输入序列?
谢谢!
有谁知道标准java优先级队列的实现细节?堆?skiplist?
我正在尝试使用优先级队列编写一个简单的应用程序.我收到以下错误 - "错误:leastPriority不是抽象的,并且不会覆盖Comparator中的抽象方法compare(Integer,Integer)"和"错误:不兼容的类型Comparator cmp = new leastPriority();"
有人可以用这段代码指出问题.
我的代码是:
class leastPriority implements Comparator<Integer> {
public int compare(Reservation x, Reservation y){
if(x.getPriority() > y.getPriority()){
return -1;
}
if(x.getPriority() < y.getPriority()){
return +1;
}
return 0;
}
}
public class prioQueue{
public static void main(String args[]){
Comparator<Reservation> cmp = new leastPriority();
PriorityQueue<Reservation> queue = new PriorityQueue<Reservation>(10,cmp);
queue.add(new Reservation(1,"Andy",10));
queue.add(new Reservation(1,"Peter",1));
queue.add(new Reservation(1,"John",4));
while(true){
Reservation r = queue.poll();
if(r==null){
break;
}
System.out.println(r.getName());
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从CLRS实现Dijsktra的算法 - 算法入门书,但是,我在实现带Comparator接口的优先级队列方面遇到了麻烦.这是我的Vertex课程,你可以看到;
public class Vertex {
public boolean explored;
public int vertexID;
public LinkedList<Vertex> adjacencyList;
public LinkedList<Edge> edgeSet;
public int shortestDistance;
public Vertex predecessor;
public Vertex(int vertexID){
this.vertexID = vertexID;
this.explored = false;
this.adjacencyList = new LinkedList<>();
this.edgeSet = new LinkedList<>();
this.shortestDistance = Integer.MAX_VALUE;
this.predecessor = null;
}
}
Run Code Online (Sandbox Code Playgroud)
所以最初shortestDistance属性被声明为Integer.MAX_VALUE.此外,您可以看到从Comparator实现的类用于优先级队列.
public class WeightComparator implements Comparator<Vertex> {
@Override
public int compare(Vertex o1, Vertex o2) {
return Math.min(o1.shortestDistance, o2.shortestDistance);
}
}
Run Code Online (Sandbox Code Playgroud)
我确信整个实现由于我的一些测试没有任何逻辑错误,但是,在某些测试中它失败了.我用这个语句创建了对队列的引用
PriorityQueue<Vertex> queue = …
我正在研究霍夫曼.但我发现PriorityQueue的排序算法存在问题; 它没有足够的比较!然后我写了一个简单的类来测试Collections的排序和PriorityQueue的排序:
public class Student implements Comparable<Student>{
String name;
int score;
int math;
public Student(int score, int math, String name) {
this.name = name;
this.score = score;
this.math = math;
}
public int compareTo(Student other) {
if (this.score > other.score) {
return -1;
} else if (this.score < other.score) {
return 1;
} else {
if (this.math > other.math) {
return -1;
} else {
return 1;
}
}
return 0;
}
public String toString() {
return("[" + name + …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用列表作为保存datanode对象的优先级队列的基础容器.它似乎使用vector或deque工作正常,但我尝试使用列表作为底层容器,我尝试将一些东西推入队列我得到错误:
错误3错误C2784:'unknown-type std :: operator - (std :: move_iterator <_RanIt>&,const std :: move_iterator <_RanIt2>&)':无法推断'std :: move_iterator <_RanIt>的模板参数&'from'std :: _ List_unchecked_iterator >>'
struct datanode{
int depth;
int cost;
const int SIZE = 10;
int ident[9];
int parent;
datanode(int dep, int id[9], int);
datanode(int dep, int id[9], int, int);
datanode(const datanode&);
datanode();
datanode& datanode::operator=(const datanode&);
};
class NodeComparison
{
public:
bool operator() (datanode& da, datanode& db)
{
return da.cost > db.cost;
}
};
int main(){
std::priority_queue<datanode,list<datanode>, NodeComparison> PQueue;
int a[10] = {1,2,3,4,5,6,7,8,9,10}; …Run Code Online (Sandbox Code Playgroud) 我找不到有关如何在优先级队列中订购对象的任何信息.我试过这个:
class Person {
...
public:
bool operator<(const Person& p) {
return age < p.age;
}
}
int main() {
priority_queue<Person*> people;
people.push(new Person("YoungMan", 21));
people.push(new Person("Grandma", 83));
people.push(new Person("TimeTraveler", -5000));
people.push(new Person("Infant", 1));
while (!people.empty()) {
cout << people.top()->name;
delete people.top();
people.pop();
}
Run Code Online (Sandbox Code Playgroud)
并且它应该根据年龄给予优先级(老年人获得更高的优先级,因此将队列排在第一位),但它不起作用.但是我得到了这个输出:
Infant
Grandma
TimeTraveler
YoungMan
Run Code Online (Sandbox Code Playgroud)
我不知道这是什么命令,但它绝对不是年龄.
我希望这可以根据价格排序...
final case class Case(price: Int) {}
Run Code Online (Sandbox Code Playgroud)
但这实际上是一个更大的案例类,我从中删除了字段。我想这样排序...
val queue = PriorityQueue.empty[Case](Ordering.by((_: Case).price).reverse)
Run Code Online (Sandbox Code Playgroud)
^按降序排序。
现在我希望这种排序保持不变...
queue.enqueue(Case(price = 2))
println(queue.toString)
queue.enqueue(Case(price = 3))
println(queue.toString)
queue.enqueue(Case(price = 4))
println(queue.toString)
queue.enqueue(Case(price = 1))
println(queue.toString)
queue.enqueue(Case(price = 0))
println(queue.toString)
Run Code Online (Sandbox Code Playgroud)
但是我的输出没有在第四和第五行排序...
PriorityQueue(Case(2))
PriorityQueue(Case(2), Case(3))
PriorityQueue(Case(2), Case(3), Case(4))
PriorityQueue(Case(1), Case(2), Case(4), Case(3))
PriorityQueue(Case(0), Case(1), Case(4), Case(3), Case(2))
Run Code Online (Sandbox Code Playgroud)
而且,该foreach方法没有按顺序迭代...
queue.foreach{ q =>
print(q + ", ")
}
Run Code Online (Sandbox Code Playgroud)
打印...
Case(0), Case(1), Case(4), Case(3), Case(2),
Run Code Online (Sandbox Code Playgroud)
如何使我的队列保持降序排列?
说我有这段代码:
q = PriorityQueue()
a = ((1,1), 10, 0)
b = ((2,2), 99, 200)
q.push(a, 1)
q.push(b, 2)
Run Code Online (Sandbox Code Playgroud)
我想检查元素(1,1)是否存在于队列中的任何元组中。有没有办法做到这一点?
priority-queue ×10
java ×6
queue ×4
c++ ×2
collections ×2
comparable ×2
comparator ×2
algorithm ×1
arraylist ×1
list ×1
performance ×1
python ×1
scala ×1
sorting ×1