CKTIS / Java
ArrayDeque — a double-ended queue and FIFO processing
ArrayDeque is a double-ended queue backed by a resizable array. Learn size(), isEmpty(), addLast(), and removeFirst() to process elements in FIFO order and recognize an empty queue.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
ArrayDeque<E> deque = new ArrayDeque<>();
int count = deque.size();
boolean empty = deque.isEmpty();
deque.addLast(element);
E first = deque.removeFirst();java.util.ArrayDeque<E> implements Deque<E> and stores elements in a resizable array. A double-ended queue supports operations at both ends. Combining addLast(E) with removeFirst() produces FIFO: the element added first is removed first.
ArrayDeque.size() returns the element count as an int, not the array capacity. ArrayDeque.isEmpty() returns true when the deque contains no elements. ArrayDeque.addLast(E) appends an element at the end and returns void. ArrayDeque.removeFirst() removes and returns the element at the front.
Calling removeFirst() on an empty deque throws NoSuchElementException; an alternative is pollFirst(), which returns null in that case. Inserting null is prohibited and throws NullPointerException. The class is not thread-safe: checking isEmpty() before removal does not replace synchronization when sharing the deque.
The example processes A before B and finishes with size 0. This entry describes the Java SE 21 API; neither the repository runtime nor the production runtime has been attested.
Examples
import java.util.ArrayDeque;
public class Main {
public static void main(String[] args) {
ArrayDeque<String> queue = new ArrayDeque<>();
System.out.println(queue.isEmpty());
queue.addLast("A");
queue.addLast("B");
System.out.println(queue.size());
while (!queue.isEmpty()) {
System.out.println(queue.removeFirst());
}
System.out.println(queue.size());
}
}Result
true 2 A B 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.