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

2022-09-02 09:27:34

这是我用于堆栈/队列分配的主要方法。我的队列不断出现错误,但堆栈没有。堆栈类似乎工作正常。我完全被困住了。它显示“无法实例化类型队列”。任何帮助将不胜感激!

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


 }      

答案 1

在 Java 文档的接口部分中:

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

这样就无法直接实例化接口 Queue<E>。但是,您仍然可以通过接口的类型来引用实现接口的对象,例如:Queue

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

你可以根据你的要求选择适当的实现,这里有一个列表,列出了Java文档中所有具体和已知的实现类:

ArrayBlockingQueue, , , , , , , , , , ,ArrayDequeConcurrentLinkedDequeConcurrentLinkedQueueDelayQueueLinkedBlockingDequeLinkedBlockingQueueLinkedListLinkedTransferQueuePriorityBlockingQueuePriorityQueueSynchronousQueue


答案 2

在Java中,它是一个接口,你不能直接实例化。请参阅此处的文档。请使用类似如下的内容:QueueQueue

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

推荐