【问题标题】:Error: Type Queue Does Not Take Parameter - What's the difference between Queues and Priority Queues?错误:类型队列不带参数 - 队列和优先级队列有什么区别?
【发布时间】:2014-03-05 22:21:55
【问题描述】:

我收到错误消息类型队列不采用参数。当我将更改队列行替换为 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&lt;Integer&gt; q = new Queue&lt;Integer&gt;(); ?
  • 是的,很抱歉含糊不清。
  • 但是您可以创建队列,因为它是abstract 类。你可以在这里创建它:Queue&lt;Integer&gt; q = new PriorityQueue&lt;Integer&gt;();
  • 你可以创建Stack,因为它不是abstract

标签: java collections parameters queue stack


【解决方案1】:

如果你想使用 Queue 而不是 PriorityQueue 创建一个带有 Queue 接口的 LinkedList 类的对象。

import java.util.*;
.....
Queue<Integer> q = new LinkedList<Integer>();
.....

如果在此之后仍然无法正常工作,请尝试使用指定的导入语句:

import java.util.LinkedList;
import java.util.Queue;

有时它不会接受带有 java.util.* 的参数

code snippet of error due to java.util.*

【讨论】:

  • 最后一部分让我... import java.util.LinkedList;导入 java.util.Queue; 有时它不会接受带有 java.util 的参数
【解决方案2】:

Queue 本身不是一个具体的类(它是一个接口),因此无法实例化。但是,PriorityQueueQueue 的具体实现,因此可以实例化。

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 2011-11-10
    • 2013-09-30
    • 1970-01-01
    • 2017-04-11
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多