【问题标题】:Cannot instantiate the type Queue. Why is this?无法实例化类型队列。为什么是这样?
【发布时间】:2015-04-22 21:03:13
【问题描述】:

这是我分配堆栈/队列的主要方法。我的队列不断出错,但我的堆栈却没有。堆栈类似乎工作得很好。我完全被困住了。它说“无法实例化类型队列”。非常感激任何的帮助!

 public class mainMeth {

      public static void main(String[] args) throws FileNotFoundException {
            File Polish = new File("fILE4INPUT.txt");
            File out = new File("outfile.txt");

            Scanner f = new Scanner(Polish);
            Queue inputQ = new Queue();
            Stack stack2 = new Stack();
            Queue outputQ = new Queue();

            String word;
            Character ch;

            while (f.hasNext()) {
                String myString = f.nextLine();

                for (int count = 0; count < myString.length(); count++) {
                    ch = myString.charAt(count);
                    inputQ.addtoRear(ch);
                }
                while (!inputQ.ismtQ()) {
                    ch = inputQ.remfront();

                    if (isAlpha(ch)) {
                        // System.out.println(ch);
                        outputQ.addtoRear(ch);
                    } else {
                        if (isOperator(ch)) {

                            if (stack2.ismt()) {
                                stack2.push(ch);

                            } else {
                                if (valueOf(ch) > valueOf(stack2.top())) {
                                    stack2.push(ch);
                                } else {
                                    outputQ.addtoRear(stack2.pop());
                                    stack2.push(ch);
                                }
                            }
                        }
                    }
                }
                while (!stack2.ismt()) {
                    outputQ.addtoRear(stack2.pop());
                }
                System.out.println(outputQ.toString() + "\n\n");
                while (!outputQ.ismtQ()) {
                    outputQ.remfront();
                }

            }

        }

        public static boolean isAlpha(Character ch) {
            boolean retVal = false;
            if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
                retVal = true;


            return (retVal);
        }

        public static boolean isOperator(Character ch) {
            boolean retVal = false;
            if (ch == '+' || ch == '-' || ch == '/' || ch == '*')
                retVal = true;

            return (retVal);
        }

        public static int valueOf(Character ch) {
            int retval = 0;
            if (ch == '/' || ch == '*')
                retval = 2;
            else
                retval = 1;
            return retval;
        }


 }      

【问题讨论】:

  • 用正确的语言标记问题可能会帮助您获得一些答案。
  • Queue 的实际类(导入语句)是什么?如果只是 Java Queue 接口,则无法实例化,因为它是一个接口。

标签: java queue


【解决方案1】:

在 Java 文档的 Interfaces 部分:

接口不能被实例化——它们只能由 类或由其他接口扩展。

那么你不能直接实例化interface Queue&lt;E&gt;。但是,您仍然可以通过接口的类型来引用实现Queue 接口的对象,例如:

// As I saw that you are adding Characters to your queue
Queue<Character> inputQ = new PriorityQueue<Character>();

您可以根据您的要求选择适当的实现,这里列出了来自java docs 的所有具体和已知的实现类:

ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedList, LinkedTransferQueue, PriorityBlockingQueuePriorityQueueSynchronousQueue

【讨论】:

    【解决方案2】:

    在Java中,Queue是一个接口,不能直接实例化Queue。请参阅文档here。请使用以下内容:

    Queue<String> queue = new LinkedList<String>();
    

    【讨论】:

      【解决方案3】:

      那是因为队列是一个接口。查看 oracle 规范以细化可以实例化的具体类。 link

      【讨论】:

        【解决方案4】:

        Java Queue 是一个接口,不能实例化。您需要一个实现 Queue 的具体类。

        【讨论】:

          猜你喜欢
          • 2014-07-12
          • 2014-12-09
          • 1970-01-01
          • 1970-01-01
          • 2011-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多