ArrayDeque<E> · Java

offer / offerLast / addLast

Adds at the tail: the enqueue operation.

public boolean offerLast(E e); public void addLast(E e); boolean offer(E e) // == offerLast

Complexity

Best caseO(1)
AmortizedO(1)
Worst caseO(n) when growing
Extra spaceO(1) amortized

Store at the tail index and advance it modulo the capacity; growth copies the ring into a larger array.

Tested example

Queue<String> queue = new ArrayDeque<>();
queue.offer("first");  // Queue.offer == Deque.offerLast
queue.offer("second");
assert queue.size() == 2;
try {
    queue.offer(null);
    throw new AssertionError("expected NullPointerException");
} catch (NullPointerException expected) {
    // ArrayDeque does not accept null elements
}
Runs in the test suite on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/test/java/dev/bekadan/dsa/collections/ArrayDequeExamples.java

Parameters

e
Non-null element.

Behaviour

Returns
offer forms: true. add forms: nothing.
Errors and preconditions
NullPointerException for null.
Changes
size increases by 1; may grow the array.
Memory and invalidation
Growth allocates a new array.