首页  >  文章  >  Java  >  而在Java中

而在Java中

WBOY
WBOY原创
2024-08-30 16:03:51882浏览

Deque 是 java 中存在的一个接口。实用程序包;基本上它是队列接口的子类型。通常deque的意思是双端队列,也就是说我们可以从前后两端进行插入和删除操作。在数据结构deque中,我们可以将其视为队列(先进先出,数据结构),也可以将其视为堆栈(后进先出,数据结构)。在deque中,我们不能创建对象,因为deque是一个接口,所以我们总是需要创建一个类。与其他队列类型相比,Deque 提供了更好的选择,并且具有更多优势。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

Deque que =new Linkedlist();

说明

我们首先需要创建该类的实例来实现双端队列,因此这里我们创建了该 LinkedList 的一个新实例,如上面的语法所示。我们还可以使用数组创建双端队列,如下所示。

Deque que =new ArrayDeque();

说明 在上面的语法中,我们使用 Arraydeque 数组创建了一个类的实例,如上面的语法所示。

Deque 在 Java 中是如何工作的?

现在让我们看看双端队列在 Java 中是如何工作的,如下所示。通常在队列中,我们可以从后端添加元素,也可以从前端删除元素,但在双端队列中,我们可以从双端队列的两端执行这两种操作。在 Java Deque 中,您需要启动该接口的可靠执行才能使用它。您可以在 Java Collections API 中随附的 Deque 执行之间进行选择:

java.util.LinkedList
java.util.ArrayDeque

LinkedList 类是一个漂亮的标准 Deque 和 Queue 执行。它利用内部的连接列表来显示行或双端队列。

Java ArrayDeque 类将其组件存储在集群中。如果组件的数量超过了集群中的空间,则分配另一个展品,并将所有组件移至此处。因此,ArrayDeque 是根据具体情况进行开发的,无论其组件是否存储在展览中。

双端队列方法

Deque 扩展了 Queue 接口;它继承了 Queue 接口的每一项策略。

除了 Queue 接口中可以访问的策略之外,Deque 接口还包含以下技术:

  • addFirst(): 用于将预定义组件添加到双端队列的开头。有时,如果双端队列已满,双端队列会抛出特殊情况。
  • addLast(): 用于将预定义组件添加到双端队列的末尾。有时,如果双端队列已满,双端队列会抛出特殊情况。
  • offerFirst(): 它用于将预定义组件添加到双端队列的开头,有时如果双端队列已满,它会返回 bogus。
  • offerLast(): 用于将预定组件添加到双端队列的末尾,有时如果双端队列已满,它会返回 bogus。
  • getFirst(): 基本上它用于返回双端队列的第一个组件,如果双端队列为空,则显示一个异常,即双端队列为空。
  • getLast(): 基本上它用于返回双端队列的最后一个组件,如果双端队列为空,则显示一个异常,即双端队列为空。
  • peekFirst(): 基本上它用于返回双端队列的第一个组件,如果双端队列为空,则返回 null。
  • peekLast(): 基本上它用于返回双端队列的最后一个组件,如果双端队列为空则返回 null。
  • removeFirst(): 用于删除双端队列的第一个组件,如果双端队列为空,则显示异常。
  • removeLast(): 用于删除双端队列的最后一个组件,如果双端队列为空,则显示异常。
  • pollFirst(): 基本上它用于返回双端队列的第一个组件,如果双端队列为空,则返回 null。
  • pollLast(): 基本上它用于返回双端队列的最后一个组件,如果双端队列为空,则返回 null。

双端队列作为堆栈数据结构

Java Collections 系统的 Stack 类给出了堆栈的执行。

有时,规定使用 Deque 作为堆栈而不是 Stack 类。以下是 Deque 接口提供的执行堆栈的技术:

  • push(): It is used to add a component toward the beginning of deque.
  • pop(): It is used to remove a component from the beginning of deque.
  • peek(): It is used to return a component from the beginning of deque.

Examples of Deque in Java

Now let’s see the difference of Deque in Java as follows.

import java.util.Deque;
import java.util.ArrayDeque;
class dque {
public static void main(String[] args) {
// creating Deque by using the ArrayDeque class as below
Deque<Integer> add = new ArrayDeque<>();
// Here we add values or we can say that component to the Deque
add.offer(5);
add.offerLast(4);
add.offerFirst(6);
System.out.println("Deque: " + add);
// Here access component from the Deque
int firstCompo = add.peekFirst();
System.out.println("First Component of Deque: " + firstCompo);
int lastCompo = add.peekLast();
System.out.println("Last Component of Deque: " + lastCompo);
// Here we remove component from the Deque
int revNum1 = add.pollFirst();
System.out.println("Removed First Component from the deque: " + revNum1);
int revNum2 = add.pollLast();
System.out.println("Removed last Component from the deque: " + revNum2);
System.out.println("Modified Deque is that: " + add);
}
}

Explanation

In the above example, we try to implement deque by using the ArrayDeque, in the above example, we try to insert the value at the first position and last position of deque as shown in the above example. Here we also access the deque value by using the peekLat () and pollFirst method as well as we also remove the value from the deque by using the pollFirst and pollLast() method. The end output of the code we illustrate by using the following screenshot.

而在Java中

The same way we can implement deque by using LinkedList.

Conclusion

We hope from this article you learn the Deque in Java. From the above article, we have learned the basic syntax of Deque in Java and we also see different examples of Deque. From this article, we learned how and when we use the Deque in Java.

以上是而在Java中的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn