当前位置:   article > 正文

java双端队列_解释java双端队列

java 双端队列是什么

java双端队列

The Deque is a line with double ends. This can add and remove data elements from either head or tail from a data structure. This can either be used as a FIFO, or as a LIFO. FIFO and LIFO representations are Queue and Stack in Java respectively.

双端队列是具有两端的线。 这可以从数据结构的头部或尾部添加和删除数据元素。 可以用作FIFO或LIFO。 FIFO和LIFO表示法分别是Java中的Queue和Stack。

That is how the diagrammatic nature of working feels. Moving on we’ve included several methods in the deque. Let’s look at this one.

这就是工作的图示性质。 继续,我们在双端队列中包含了几种方法。 让我们来看看这个。

Dequeuing Implementations

使实现出队

Since Java Deque is an interface you need to immediately implement the interface to use it in a practical manner. In the Java Collections API, you can choose between the following Deque implementations:

由于Java Deque是一个接口,因此您需要立即实现该接口以实际方式使用它。 在Java Collections API中,您可以在以下Deque实现中进行选择:

To get in-Depth knowledge on Java you can enroll for a live demo on Java Training

要获得有关Java的深入知识,您可以注册Java培训现场演示

LinkedList: java.util.Java.util. DequeArray

The LinkedList class is a relatively simple implementation of the Deque and Queue. It makes internal use of a linked list to model a queue or deque.

LinkedList类是Deque和Queue的相对简单的实现。 它在内部使用链接列表来建模队列或双端队列。

The Java ArrayDeque class dynamically stores its elements into an array. When the number of elements in the array exceeds the space, a new array is assigned, and all elements are passed over. In other words, even if it stores its elements in an array, the ArrayDeque grows as needed.

Java ArrayDeque类将其元素动态存储到数组中。 当数组中的元素数超过空间时,将分配一个新的数组,并将所有元素传递过来。 换句话说,即使将其元素存储在数组中,ArrayDeque也会根据需要增长。

Build a Deque in Java

用Java构建双端队列

Before using a Java Deque you need to build an instance of one of the classes implementing the Deque interface. Here is an example of creating an instance of Java Deque by creating an instance of LinkedList:

在使用Java Deque之前,您需要构建实现Deque接口的类之一的实例。 这是通过创建LinkedList实例来创建Java Deque实例的示例:

Deque deque = LinkedList( )new;

Here is another example of Java Deque development by creating an instance in ArrayDeque:

这是通过在ArrayDeque中创建实例来开发Java Deque的另一个示例:

Deque dek = new ArrayDek);

Standard Deque in Java

Java中的标准双端队列

You can load any object into a Java Deque by default. Using Java Generics, however, you can restrict the types of objects that you can inject into a Deque. Take an example here.

您可以默认将任何对象加载到Java Deque中。 但是,使用Java泛型,可以限制可以注入到Deque中的对象的类型。 在这里举个例子。

Take your career to new heights of success with Java Course

Java课程将您的职业提升到成功的新高度

Deque < MyObject > deque = new LinkedList);

Now this Deque may only have instances of MyObject embedded in it. You can then access the elements without casting them, and iterate them. Here’s how things look.

现在,此双端队列可能只嵌入了MyObject的实例。 然后,您可以访问元素而无需强制转换它们并进行迭代。 这是事情的样子。

MyObject = deque.remove);For(MyPurpose: deque)/Do anObject submit ...}

It is considered good practice, when defining a Java Deque variable, to always specify a generic type. Thus the compiler will enable you to test the types that are inserted into the Deque and you don’t have to cast the objects when you delete them from the Deque again. Moreover, it becomes clearer for the next person to read your code what type of objects this Deque should contain.

在定义Java Deque变量时,始终指定通用类型是一种很好的做法。 因此,编译器使您能够测试插入到双端队列中的类型,并且当您再次从双端队列中删除它们时,不必强制转换对象。 而且,让下一个人更清楚地阅读您的代码,此Deque应该包含什么类型的对象,这变得更加清楚。

View the Java Generics Tutorial for more information about Java Generics.

查看Java泛型教程以获取有关Java泛型的更多信息。

Add Element to Deque in Java

在Java中将元素添加到双端队列

You can add elements to both the start and end of a Deque. The interface on Deque in Java contains the following methods to add elements to it:

您可以将元素添加到双端队列的开始和结尾。 Java中的Deque接口包含以下向其添加元素的方法:

The following parts will clarify such processes.

以下部分将阐明此类过程。

Add()

加()

Using the add( )method you add elements to the beginning end of a Deque. Here is an example for adding an element to a Java Deque end (tail):

使用add()方法可以将元素添加到Deque的开头。 这是向Java Deque末尾添加元素的示例:

Deque < String > deque = ArrayDeque < >( )new;Deque.add("figure 1);

If the element can not be inserted into the Deque, an exception will be thrown by the add( )process. This is different from the offer( )method which returns false if the item can not be inserted.

如果不能将元素插入到双端队列中,则add()进程将引发异常。 这与offer()方法不同,后者无法插入该项目时返回false。

Indeed the add( )method is inherited from the Queue interface.

实际上,add()方法是从Queue接口继承的。

Add last

最后添加

The addLast( )method also adds one element to a Java Deque ‘s end (tail). This is the equivalent of the add( )method inherited from the Queue interface within the Deque interface. Here is an example of adding an element to an instance in Java Deque using the method addLast):

addLast()方法还将一个元素添加到Java Deque的末尾(尾部)。 这等效于从Deque接口内的Queue接口继承的add()方法。 这是使用方法addLast将元素添加到Java Deque中的实例的示例:

Deque < String > deque = ArrayDeque < >( )new;Deque.addLast('the element 1);

If the element can not be inserted into the Deque, an exception will be thrown by the addLast( )process. This is different from the method offerLast( )which returns false when the item can not be added to the Deque.

如果不能将元素插入到双端队列中,则addLast()进程将引发异常。 这与方法offerLast()不同,后者无法将商品添加到双端队列时返回false。

Add Prime)

添加素数)

Instead of ending a Java Deque you call the addFirst( )method to add an element at the beginning (head). Here is an example for adding an element to a Java Deque ‘s start (head):

无需结束Java Deque,而是调用addFirst()方法在开头(头)添加元素。 这是将元素添加到Java Deque的开始(头)的示例:

ment 1);

ment 1);

If the element can not be added to the Deque initialization, an exception will be thrown by the addFirst( )process. This is different from the offerFirst( )method which returns false if an item can not be inserted at the Deque start.

如果不能将元素添加到Deque初始化中,则addFirst()进程将引发异常。 这与offerFirst()方法不同,后者无法在Deque开始时插入项目,则返回false。

Offer)-Offer)

提供)-提供)

An element is added to the Deque end (tail) by the offer() method. If the item add is efficient the method of offer( )returns true. If the item adding fails-e.g. if the Deque is complete, then the method of offer( )returns false. This is different from the add( )method that will throw an exception by inserting an element that fails at the end of the Deque. Here is an example of how to add an element using the offer( )method to end a Java Deque.

通过offer()方法将元素添加到双端队列末端(尾部)。 如果项目添加有效,则offer()方法将返回true。 如果项目添加失败(例如,如果双端队列已完成),则offer()方法将返回false。 这与add()方法不同,后者会通过在Deque末尾插入失败的元素来引发异常。 这是一个如何使用offer()方法添加元素以结束Java Deque的示例。

Deque < String > deque = ArrayDeque < >( )new;Deque.offer('Item 1);BidLast)

The method offerLast( )adds an element to the Deque end (tail), just like offer). The offerLast( )method name is just a little bit more telling of where the element is applied to the Deque. If the element add is successful the method of offerLast( )returns true. If the element adding fails-e.g. if the Deque is complete, then the method of offerLast( )returns false. This is different from the addLast( )method that throws an exception by adding an element to the fails at the end of the Deque. Here is an example of how to connect an item to a Java Deque end using the method offerLast):

就像offer一样,方法offerLast()将一个元素添加到Deque末尾(尾部)。 offerLast()方法名称仅说明该元素应用于Deque的位置。 如果元素添加成功,则offerLast()方法将返回true。 如果元素添加失败(例如,如果双端队列已完成),则offerLast()方法将返回false。 这与addLast()方法不同,后者通过在Deque末尾的失败中添加元素来引发异常。 这是一个如何使用方法offerLast将项目连接到Java Deque末端的示例:

Deque < String > deque = ArrayDeque < >( )new;Deque.offerLast('the 1 "element);FirstBid)

The method offerFirst( )adds an element to the Deque start (head). If the item is added successfully the offerFirst( )method returns valid. If the adding item fails-e.g. if the Deque is complete, then the method of offerFirst( )returns false. This is different from the addFirst( )method that will throw an exception by adding an element to the Deque fails initialization. Here is an example of how to add an element to the start of a Java Deque using the method offerFirst):

offerFirst()方法将一个元素添加到双端队列开始(head)。 如果项目添加成功,offerFirst()方法将返回有效。 如果添加项失败(例如,双端队列已完成),则offerFirst()方法将返回false。 这与addFirst()方法不同,后者通过在Deque中添加元素来引发初始化失败而引发异常。 这是一个如何使用方法offerFirst将元素添加到Java Deque开头的示例:

Deque < String > deque = ArrayDeque < >( )new;Deque.offerFirst('the element 1);Push)-Push)

The method push( )adds an element to the start (head) of a Java Deque process. If adding the item fails, the push( )method will throw an exception for example if the Deque is complete. This is similar to how the method addFirst( )works. Here is an example of adding an element to a Java Deque start using the method push):

push()方法将元素添加到Java Deque进程的开始(头部)。 如果添加项目失败,则例如在Deque完成时,push()方法将引发异常。 这类似于方法addFirst()的工作方式。 这是一个使用push方法将元素添加到Java Deque开始的示例:

Deque < string > deque = LinkedList < >( )new;Deque.push("set 0);

At Item in Deque, Peek

在Deque的物品上,Peek

You can peek at a Java Deque’s first and last elements. Peeking an element means getting an element reference without removing the element from the Deque. Using these methods you can look at the first and last item of a Java Deque:

您可以查看Java Deque的第一个和最后一个元素。 偷看元素意味着无需从Deque中删除元素即可获取元素引用。 使用这些方法,您可以查看Java Deque的第一项和最后一项:

The following sections should cover both of those approaches.

以下各节应涵盖这两种方法。

Peek)-Peek)

偷看)

The peek( )method returns the first element without removing it from the initial (head) of a Java Deque. If the Deque is empty it returns null to peek). Here is an example of peeking the first item in a Java Deque using the method peek):

peek()方法返回第一个元素,而不将其从Java Deque的开头(头)移除。 如果双端队列为空,则返回null以进行窥视。 这是一个使用peek方法来窥视Java Deque中第一项的示例:

Deque < String > deque = ArrayDeque < >( )new;Deque.add("Element 1);Deque.add('ultimate element);FirstElement string = for deque.peek);

The first Element will point to the first String element added to the Deque after running this code: “first element.”

运行以下代码后,第一个元素将指向添加到双端队列的第一个String元素:“第一个元素”。

Conclusion

结论

I hope you reach to a conclusion about Deque in Java. You can learn more through Java online training.

我希望您能得出有关Java中Deque的结论。 您可以通过Java在线培训了解更多信息。

翻译自: https://medium.com/@sindhujacynixit/explain-java-deque-d502b29f1492

java双端队列

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/78794
推荐阅读
相关标签
  

闽ICP备14008679号