首頁  >  文章  >  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
上一篇:Java中的佇列下一篇:Java中的佇列