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 case | O(1) |
|---|---|
| Amortized | O(1) |
| Worst case | O(n) when growing |
| Extra space | O(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
}
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.