Java中的ArrayDeque
Java中的 ArrayDeque 除了实现 Deque 接口外,还提供了一种应用 resizable-array 的方法。它也被称为Array Double Ended Queue或Array Deck 。这是一种特殊的数组,它可以增长并允许用户从队列的两侧添加或删除元素。
ArrayDeque 的几个重要特性如下:
- 数组双端队列没有容量限制,它们会根据需要增长以支持使用。
- 它们不是线程安全的,这意味着在没有外部同步的情况下,ArrayDeque 不支持多个线程的并发访问。
- ArrayDeque 中禁止使用 Null 元素。
- 当用作堆栈时,ArrayDeque 类可能比 Stack 更快。
- ArrayDeque 类在用作队列时可能比 LinkedList 更快。
ArrayDeque 实现的接口:
ArrayDeque 类实现了这两个接口:
- 队列接口:它是一个接口,它是一个先入先出数据结构,其中元素是从后面添加的。
- 双端队列接口:它是一个双端队列,您可以在其中从两侧插入元素。它是一个实现Queue的接口。
ArrayDeque 实现了 Queue 和 Deque。它可以从两侧动态调整大小。 ArrayDeque 在层次结构中的所有实现接口是Serializable , Cloneable , Iterable
语法:声明
public class ArrayDeque
extends AbstractCollection
implements Deque
Here, E refers to the element which can refer to any class, such as Integer or String class.
现在我们已经完成了语法,现在让我们在实施之前为它定义构造函数,以便更好地掌握它并更好地感知输出。
- ArrayDeque():此构造函数用于创建一个空的 ArrayDeque,默认情况下,初始容量可容纳 16 个元素。
ArrayDeque dq = new ArrayDeque();
- ArrayDeque(Collection extends E> c):该构造函数用于创建一个ArrayDeque,其中包含与指定集合相同的所有元素。
ArrayDeque dq = new ArrayDeque(Collection col);
- ArrayDeque(int numofElements):此构造函数用于创建一个空的 ArrayDeque 并保持包含指定数量元素的容量。
ArrayDeque dq = new ArrayDeque(int numofElements);
ArrayDeque 中的方法如下:
Note: Here, Element is the type of elements stored by ArrayDeque.
METHOD | DESCRIPTION |
---|---|
add(Element e) | The method inserts a particular element at the end of the deque. |
addAll(Collection extends E> c) | Adds all of the elements in the specified collection at the end of this deque, as if by calling addLast(E) on each one, in the order that they are returned by the collection’s iterator. |
addFirst(Element e) | The method inserts particular element at the start of the deque. |
addLast(Element e) | The method inserts a particular element at the end of the deque. It is similar to the add() method |
clear() | The method removes all deque elements. |
clone() | The method copies the deque. |
contains(Obj) | The method checks whether a deque contains the element or not |
element() | The method returns element at the head of the deque |
forEach(Consumer super E> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
getFirst() | The method returns first element of the deque |
getLast() | The method returns last element of the deque |
isEmpty() | The method checks whether the deque is empty or not. |
iterator() | Returns an iterator over the elements in this deque. |
offer(Element e) | The method inserts element at the end of deque. |
offerFirst(Element e) | The method inserts element at the front of deque. |
offerLast(Element e) | The method inserts element at the end of the deque. |
peek() | The method returns head element without removing it. |
poll() | The method returns head element and also removes it |
pop() | The method pops out an element for stack represented by deque |
push(Element e) | The method pushes an element onto stack represented by deque |
remove() | The method returns head element and also removes it |
remove(Object o) | Removes a single instance of the specified element from this deque. |
removeAll(Collection> c) | Removes all of this collection’s elements that are also contained in the specified collection (optional operation). |
removeFirst() | The method returns the first element and also removes it |
removeFirstOccurrence(Object o) | Removes the first occurrence of the specified element in this deque (when traversing the deque from head to tail). |
removeIf(Predicate super Element> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
removeLast() | The method returns the last element and also removes it |
removeLastOccurrence(Object o) | Removes the last occurrence of the specified element in this deque (when traversing the deque from head to tail). |
retainAll(Collection> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
size() | Returns the number of elements in this deque. |
spliterator() | Creates a late-binding and fail-fast Spliterator over the elements in this deque. |
toArray() | Returns an array containing all of the elements in this deque in proper sequence (from first to the last element). |
toArray(T[] a) | Returns an array containing all of the elements in this deque in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array. |
从类Java.util.AbstractCollection 继承的方法 Method Action Performed containsAll(Collection c) Returns true if this collection contains all of the elements in the specified collection. toString() Returns a string representation of this collection.
从接口Java.util.Collection 继承的方法 Method Action Performed containsAll(Collection c) Returns true if this collection contains all of the elements in the specified collection. equals() Compares the specified object with this collection for equality. hashcode() Returns the hash code value for this collection. parallelStream() Returns a possibly parallel Stream with this collection as its source. stream() Returns a sequential Stream with this collection as its source. toArray(IntFunction Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.
在接口Java.util.Deque 中声明的方法 Method Action Performed descendingIterator() Returns an iterator over the elements in this deque in reverse sequential order. peekFirst() Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty. peekLast() Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty. pollFirst() Retrieves and removes the first element of this deque, or returns null if this deque is empty. pollLast() Retrieves and removes the last element of this deque, or returns null if this deque is empty.
例子
Java
// Java program to Implement ArrayDeque in Java
//
// Importing utility classes
import java.util.*;
// ArrayDequeDemo
public class GGFG {
public static void main(String[] args)
{
// Creating and initializing deque
// Declaring object of integer type
Deque de_que = new ArrayDeque(10);
// Operations 1
// add() method
// Adding custom elements
// using add() method to insert
de_que.add(10);
de_que.add(20);
de_que.add(30);
de_que.add(40);
de_que.add(50);
// Iterating using for each loop
for (Integer element : de_que) {
// Print the corresponding element
System.out.println("Element : " + element);
}
// Operation 2
// clear() method
System.out.println("Using clear() ");
// Clearing all elements using clear() method
de_que.clear();
// Operations 3
// addFirst() method
// Inserting at the start
de_que.addFirst(564);
de_que.addFirst(291);
// Operation 4
// addLast() method
// Inserting at end
de_que.addLast(24);
de_que.addLast(14);
// Display message
System.out.println(
"Above elements are removed now");
// Iterators
// Display message
System.out.println(
"Elements of deque using Iterator :");
for (Iterator itr = de_que.iterator();
itr.hasNext();) {
System.out.println(itr.next());
}
// descendingIterator()
// To reverse the deque order
System.out.println(
"Elements of deque in reverse order :");
for (Iterator dItr = de_que.descendingIterator();
dItr.hasNext();) {
System.out.println(dItr.next());
}
// Operation 5
// element() method : to get Head element
System.out.println(
"\nHead Element using element(): "
+ de_que.element());
// Operation 6
// getFirst() method : to get Head element
System.out.println("Head Element using getFirst(): "
+ de_que.getFirst());
// Operation 7
// getLast() method : to get last element
System.out.println("Last Element using getLast(): "
+ de_que.getLast());
// Operation 8
// toArray() method :
Object[] arr = de_que.toArray();
System.out.println("\nArray Size : " + arr.length);
System.out.print("Array elements : ");
for (int i = 0; i < arr.length; i++)
System.out.print(" " + arr[i]);
// Operation 9
// peek() method : to get head
System.out.println("\nHead element : "
+ de_que.peek());
// Operation 10
// poll() method : to get head
System.out.println("Head element poll : "
+ de_que.poll());
// Operation 11
// push() method
de_que.push(265);
de_que.push(984);
de_que.push(2365);
// Operation 12
// remove() method : to get head
System.out.println("Head element remove : "
+ de_que.remove());
System.out.println("The final array is: " + de_que);
}
}
Java
// Java program to Illustrate Addition of elements
// in ArrayDeque
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// AddingElementsToArrayDeque
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Initializing a deque
// since deque is an interface
// it is assigned the
// ArrayDeque class
Deque dq = new ArrayDeque();
// add() method to insert
dq.add("The");
dq.addFirst("To");
dq.addLast("Geeks");
// offer() method to insert
dq.offer("For");
dq.offerFirst("Welcome");
dq.offerLast("Geeks");
// Printing Elements of ArrayDeque to the console
System.out.println("ArrayDeque : " + dq);
}
}
Java
// Java program to Access Elements of ArrayDeque
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// AccessingElementsOfArrayDeque
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty ArrayDeque
ArrayDeque de_que
= new ArrayDeque();
// Using add() method to add elements into the Deque
// Custom input elements
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Displaying the First element
System.out.println("The first element is: "
+ de_que.getFirst());
// Displaying the Last element
System.out.println("The last element is: "
+ de_que.getLast());
}
}
Java
// Java program to Illustrate Removal Elements in Deque
// Importing all utility classes
import java.util.*;
// RemoveElementsOfArrayDeque
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Initializing a deque
Deque dq = new ArrayDeque();
// add() method to insert
dq.add("One");
// addFirst inserts at the front
dq.addFirst("Two");
// addLast inserts at the back
dq.addLast("Three");
// print elements to the console
System.out.println("ArrayDeque : " + dq);
// remove element as a stack from top/front
System.out.println(dq.pop());
// remove element as a queue from front
System.out.println(dq.poll());
// remove element from front
System.out.println(dq.pollFirst());
// remove element from back
System.out.println(dq.pollLast());
}
}
Java
// Java program to Illustrate Iteration of Elements
// in Deque
// Importing all utility classes
import java.util.*;
// Main class
// IterateArrayDeque
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing an deque
Deque dq = new ArrayDeque();
// Adding elements at the back
// using add() method
dq.add("For");
// Adding element at the front
// using addFirst() method
dq.addFirst("Geeks");
// add element at the last
// using addLast() method
dq.addLast("Geeks");
dq.add("is so good");
// Iterate using Iterator interface
// from the front of the queue
for (Iterator itr = dq.iterator(); itr.hasNext();) {
// Print the elements
System.out.print(itr.next() + " ");
}
// New line
System.out.println();
// Iterate in reverse sequence in a queue
for (Iterator itr = dq.descendingIterator();
itr.hasNext();) {
System.out.print(itr.next() + " ");
}
}
}
Element : 10
Element : 20
Element : 30
Element : 40
Element : 50
Using clear()
Above elements are removed now
Elements of deque using Iterator :
291
564
24
14
Elements of deque in reverse order :
14
24
564
291
Head Element using element(): 291
Head Element using getFirst(): 291
Last Element using getLast(): 14
Array Size : 4
Array elements : 291 564 24 14
Head element : 291
Head element poll : 291
Head element remove : 2365
The final array is: [984, 265, 564, 24, 14]
如果此示例中的清晰度有些滞后,如果是这样,那么我们提出了对 ArrayDeque 类的各种操作让我们看看如何在 ArrayDeque 上执行一些常用操作,以更好地理解我们上面使用的操作说明 Array Deque 作为一个整体。
- 添加操作
- 访问操作
- 删除操作
- 遍历双端队列
让我们通过提供干净的Java程序来实现每个操作,如下所示:
操作 1:添加元素
为了向 ArrayDeque 添加元素,我们可以使用方法 add()、addFirst()、addLast()、offer()、offerFirst()、offerLast() 方法。
- 添加()
- addFirst()
- 添加最后()
- 提供()
- 报价第一()
- 报价最后()
例子
Java
// Java program to Illustrate Addition of elements
// in ArrayDeque
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// AddingElementsToArrayDeque
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Initializing a deque
// since deque is an interface
// it is assigned the
// ArrayDeque class
Deque dq = new ArrayDeque();
// add() method to insert
dq.add("The");
dq.addFirst("To");
dq.addLast("Geeks");
// offer() method to insert
dq.offer("For");
dq.offerFirst("Welcome");
dq.offerLast("Geeks");
// Printing Elements of ArrayDeque to the console
System.out.println("ArrayDeque : " + dq);
}
}
ArrayDeque : [Welcome, To, The, Geeks, For, Geeks]
操作 2:访问元素
添加元素后,如果我们希望访问元素,可以使用内置方法,如 getFirst()、getLast() 等。
- 获取第一()
- 获取最后()
- 窥视()
- 窥视第一()
- peekLast()
例子
Java
// Java program to Access Elements of ArrayDeque
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// AccessingElementsOfArrayDeque
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty ArrayDeque
ArrayDeque de_que
= new ArrayDeque();
// Using add() method to add elements into the Deque
// Custom input elements
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Displaying the First element
System.out.println("The first element is: "
+ de_que.getFirst());
// Displaying the Last element
System.out.println("The last element is: "
+ de_que.getLast());
}
}
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The first element is: Welcome
The last element is: Geeks
操作3.移除元素
为了从双端队列中删除元素,有多种方法可用。由于我们也可以从两端删除,所以 deque 接口为我们提供了 removeFirst()、removeLast() 方法。除此之外,该接口还为我们提供了 poll()、pop()、pollFirst()、pollLast() 方法,其中 pop() 用于删除和返回双端队列的头部。但是,使用 poll() 是因为它提供了与 pop() 相同的功能,并且当双端队列为空时不会返回异常。这些操作集如下所列:
- 消除()
- 删除第一()
- 删除最后()
- 轮询()
- 民意调查()
- 民意调查()
- 流行音乐()
例子
Java
// Java program to Illustrate Removal Elements in Deque
// Importing all utility classes
import java.util.*;
// RemoveElementsOfArrayDeque
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Initializing a deque
Deque dq = new ArrayDeque();
// add() method to insert
dq.add("One");
// addFirst inserts at the front
dq.addFirst("Two");
// addLast inserts at the back
dq.addLast("Three");
// print elements to the console
System.out.println("ArrayDeque : " + dq);
// remove element as a stack from top/front
System.out.println(dq.pop());
// remove element as a queue from front
System.out.println(dq.poll());
// remove element from front
System.out.println(dq.pollFirst());
// remove element from back
System.out.println(dq.pollLast());
}
}
ArrayDeque : [Two, One, Three]
Two
One
Three
null
操作4:遍历 Deque
由于 deque 可以从两个方向迭代,所以 deque 接口的 iterator 方法为我们提供了两种迭代方式。一个从第一个,另一个从后面。下面列出了这些操作集:
- 消除()
- 迭代器()
- 降序迭代器()
例子
Java
// Java program to Illustrate Iteration of Elements
// in Deque
// Importing all utility classes
import java.util.*;
// Main class
// IterateArrayDeque
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing an deque
Deque dq = new ArrayDeque();
// Adding elements at the back
// using add() method
dq.add("For");
// Adding element at the front
// using addFirst() method
dq.addFirst("Geeks");
// add element at the last
// using addLast() method
dq.addLast("Geeks");
dq.add("is so good");
// Iterate using Iterator interface
// from the front of the queue
for (Iterator itr = dq.iterator(); itr.hasNext();) {
// Print the elements
System.out.print(itr.next() + " ");
}
// New line
System.out.println();
// Iterate in reverse sequence in a queue
for (Iterator itr = dq.descendingIterator();
itr.hasNext();) {
System.out.print(itr.next() + " ");
}
}
}
Geeks For Geeks is so good
is so good Geeks For Geeks
相关文章:
- Java Java类设置 1
- Java Java类设置 2
- Java中的ArrayList与LinkedList