我收到错误消息类型队列不采用参数。当我将更改队列行更改为优先级队列时,此错误消失了,并且可以正常编译。有什么区别,如何将其更改为编译和常规队列?
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接口创建LinkedList类的对象。
import java.util.*;
.....
Queue<Integer> q = new LinkedList<Integer>();
.....
如果在此之后它仍然不起作用,请尝试使用指定的导入语句:
import java.util.LinkedList;
import java.util.Queue;
有时它不会接受java.util的参数。
由于 java.util 而导致的错误代码片段。
Queue
本身不是一个具体的类(它是一个接口),因此不能实例化。 但是,PriorityQueue
是Queue
的具体实现,因此可以实例化。