简体   繁体   English

如何在java中实例化一个队列对象?

[英]How do I instantiate a Queue object in java?

When I try:当我尝试:

Queue<Integer> q = new Queue<Integer>();

the compiler is giving me an error.编译器给了我一个错误。 Any help?有什么帮助吗?

Also, if I want to initialize a queue do I have to implement the methods of the queue?另外,如果我想初始化一个队列,我是否必须实现队列的方法?

Queue is an interface. Queue是一个接口。 You can't instantiate an interface directly except via an anonymous inner class.除非通过匿名内部类,否则不能直接实例化接口。 Typically this isn't what you want to do for a collection.通常,这不是您想要为集合执行的操作。 Instead, choose an existing implementation.相反,选择一个现有的实现。 For example:例如:

Queue<Integer> q = new LinkedList<Integer>();

or

Queue<Integer> q = new ArrayDeque<Integer>();

Typically you pick a collection implementation by the performance and concurrency characteristics you're interested in.通常,您会根据您感兴趣的性能和并发特性来选择集合实现。

A Queue is an interface, which means you cannot construct a Queue directly. Queue是一个接口,这意味着您不能直接构造Queue

The best option is to construct off a class that already implements the Queue interface, like one of the following: AbstractQueue , ArrayBlockingQueue , ArrayDeque , ConcurrentLinkedQueue , DelayQueue , LinkedBlockingQueue , LinkedList , PriorityBlockingQueue , PriorityQueue , or SynchronousQueue .最好的选择是构造断一类已经实现了Queue接口,如下列情况之一: AbstractQueueArrayBlockingQueueArrayDequeConcurrentLinkedQueueDelayQueueLinkedBlockingQueueLinkedListPriorityBlockingQueuePriorityQueue ,或SynchronousQueue

An alternative is to write your own class which implements the necessary Queue interface.另一种方法是编写自己的类来实现必要的 Queue 接口。 It is not needed except in those rare cases where you wish to do something special while providing the rest of your program with a Queue .除非在极少数情况下您希望在为程序的其余部分提供Queue同时做一些特殊的事情,否则不需要它。

public class MyQueue<T extends Tree> implements Queue<T> {
   public T element() {
     ... your code to return an element goes here ...
   }

   public boolean offer(T element) {
     ... your code to accept a submission offer goes here ...
   }

   ... etc ...
}

An even less used alternative is to construct an anonymous class that implements Queue .一个更少使用的替代方法是构造一个实现Queue的匿名类。 You probably don't want to do this, but it's listed as an option for the sake of covering all the bases.您可能不想这样做,但为了涵盖所有基础,它被列为一个选项。

new Queue<Tree>() {
   public Tree element() {
     ...
   };

   public boolean offer(Tree element) {
     ...
   };
   ...
};
Queue<String> qe=new LinkedList<String>();

qe.add("b");
qe.add("a");
qe.add("c");

Since Queue is an interface, you can't create an instance of it as you illustrated由于Queue是一个接口,因此您无法按照说明创建它的实例

Queue is an interface;队列是一个接口; you can't explicitly construct a Queue.你不能显式构造一个队列。 You'll have to instantiate one of its implementing classes.您必须实例化其实现类之一。 Something like:类似的东西:

Queue linkedList = new LinkedList();

Here's a link to the Java tutorial on this subject. 这是有关此主题的 Java 教程的链接。

在此处输入图片说明

The Queue interface extends java.util.Collection with additional insertion, extraction, and inspection operations like: Queue 接口通过附加的插入、提取和检查操作扩展了 java.util.Collection,例如:

+offer(element: E): boolean // Inserting an element +offer(element: E): boolean //插入一个元素

+poll(): E // Retrieves the element and returns NULL if queue is empty +poll(): E //检索元素并在队列为空时返回 NULL

+remove(): E // Retrieves and removes the element and throws an Exception if queue is empty +remove(): E //检索并移除元素,如果队列为空则抛出异常

+peek(): E // Retrieves,but does not remove, the head of this queue, returning null if this queue is empty. +peek(): E //检索但不删除此队列的头部,如果此队列为空,则返回 null。

+element(): E // Retrieves, but does not remove, the head of this queue, throws an exception if te queue is empty. +element(): E //检索但不移除此队列的头部,如果队列为空则抛出异常。

Example Code for implementing Queue:实现队列的示例代码:

java.util.Queue<String> queue = new LinkedList<>();
queue.offer("Hello");
queue.offer("StackOverFlow");
queue.offer("User");

System.out.println(queue.peek());

while (queue.size() > 0){
    System.out.println(queue.remove() + " ");
}
//Since Queue is empty now so this will return NULL
System.out.println(queue.peek());

Output Of the code :代码的输出:

Hello
Hello 
StackOverFlow 
User 
null

Queue is an interface in java, you can not do that.队列是java中的一个接口,你不能那样做。

Instead you have two options:相反,您有两个选择:

option1:选项1:

Queue<Integer> Q = new LinkedList<>();

option2:选项2:

Queue<Integer> Q = new ArrayDeque<>();

I recommend using option2 as it is bit faster than the other我建议使用 option2,因为它比另一个快一点

Queue in Java is defined as an interface and many ready-to-use implementation is present as part of JDK release. Java 中的队列被定义为一个接口,并且许多现成的实现作为 JDK 版本的一部分出现。 Here are some: LinkedList , Priority Queue, ArrayBlockingQueue, ConcurrentLinkedQueue, Linked Transfer Queue, Synchronous Queue etc.这里有一些: LinkedList 、Priority Queue、ArrayBlockingQueue、ConcurrentLinkedQueue、Linked Transfer Queue、 Synchronous Queue等。

SO You can create any of these class and hold it as Queue reference.所以您可以创建这些类中的任何一个并将其作为队列引用。 for example例如

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

public class QueueExample {

 public static void main (String[] args) {
  Queue que = new LinkedList();
  que.add("first");
  que.offer("second");
  que.offer("third");
  System.out.println("Queue Print:: " + que);
  
  String head = que.element();
  System.out.println("Head element:: " + head);
  
  String element1 = que.poll();
  System.out.println("Removed Element:: " + element1);
  
  System.out.println("Queue Print after poll:: " + que);
  String element2 = que.remove();
  System.out.println("Removed Element:: " + element2);
  
  System.out.println("Queue Print after remove:: " + que);  
 }
}

You can also implement your own custom Queue implementing Queue interface.您还可以实现自己的自定义 Queue 实现 Queue 接口。

Queue is an interface in java, you could not do that. Queue是 Java 中的一个接口,你不能那样做。 try:尝试:

Queue<Integer> Q = new LinkedList<Integer>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM