![](/img/trans.png)
[英]I am new with working with priority queues and have formatted this code wrong.What is the error in my Priority Queue code?
[英]Error: Type Queue Does Not Take Parameter - What's the difference between Queues and Priority Queues?
我收到錯誤消息類型隊列不接受參數。 當我將更改隊列行替換為 PriorityQueue 時,此錯誤消失並且編譯正常。 有什么區別,如何將其更改為編譯和常規隊列?
import java.util.*;
public class StackOneTwoMultiply {
public static void main(String[] args) {
int first, second;
Stack<Integer> s = new Stack<Integer>(); //stack called s
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
for (int i = 10; i > 0; i--) { //filling the stack (what order to fill was not specifified)
s.push(i);
}
while (!s.isEmpty()) {
first = s.pop();
second = s.pop();
q.offer(first * second);
System.out.println(q.peek());
}
System.out.print(q);
}
}
Queue
本身不是一個具體的類(它是一個接口),因此不能被實例化。 然而, PriorityQueue
是Queue
的具體實現,因此可以實例化。
如果你想使用 Queue 而不是 PriorityQueue 創建和具有 Queue 接口的 LinkedList 類的對象。
import java.util.*;
.....
Queue<Integer> q = new LinkedList<Integer>();
.....
如果在此之后它仍然不起作用,請嘗試使用指定的導入語句:
import java.util.LinkedList;
import java.util.Queue;
有時它不會接受帶有 java.util.* 的參數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.