Java 无法实例化类型队列。为什么是这样?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28641605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:32:47  来源:igfitidea点击:

Cannot instantiate the type Queue. Why is this?

javaqueue

提问by Shuijiao

This is my main method for a stacks/queues assignment. I keep getting an error with my queue, but not my Stack. The stack class seems to be working just fine. I'm completely stuck. It says "cannot instantiate type Queue". Any help would be most appreciated!

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

 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;
        }


 }      

回答by Tachyon

In Java, Queueis an interface, you can't instantiate Queuedirectly. See the documentation here. Please use something like this:

在Java中,Queue是一个接口,不能Queue直接实例化。请参阅此处的文档。请使用这样的东西:

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

回答by CodingCodetta

that is because queue is an interface. check out the oracle specs to fine the a concrete class that can be instantiated. link

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

回答by Astra Bear

Java Queue is an interface and cannot be instatiated. You need a concrete class that implements Queue.

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

回答by Tarik

In the Interfacessection of the Java docs:

在Java 文档的接口部分:

Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

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

Then you can't directly instantiate the interface Queue<E>. But, you still can refer to an object that implements the Queueinterface by the type of the interface, like:

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

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

You can chose the adequate implementation to use regarding your requirements, here is a list of all concrete and known implementing Classes from it's java docs:

您可以根据您的要求选择适当的实现,这里是它的java 文档中所有具体和已知实现类的列表:

ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedList, LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue, SynchronousQueue

ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedList, LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue,SynchronousQueue