In Java, addLast(e) inserts an element at the tail (the end) of an ArrayDeque. The elements already in the deque stay where they are. The method returns void.
It is the same operation as add(e), except that add returns true. offerLast(e) also inserts at the tail and returns true.
An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety. Since Java 21, addLast is also declared by the SequencedCollection interface, which Deque extends.
Syntax
Call addLast with the element that should become the new tail.
addLast pairs naturally with removeFirst or pollFirst for a first-in, first-out queue, and with pollLast for a stack that grows at the tail.
Library declarations
Java
// java.util.ArrayDeque<E>
public void addLast(E e)
// Declared in Deque<E> (and SequencedCollection<E> since Java 21)
Parameters
Parameter
Description
e
Required. The element to insert at the tail. Must not be null.
Return Value
Return Value
Description
void
Nothing is returned. The deque has one more element, at the tail.
Throw or Return a Special Value?
Each end has a method that throws when it cannot work (such as on an empty deque) and one that returnsnull or false instead. The highlighted cell matches addLast().
Tickets are added at the tail and served from the head.
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeAddLastDemo {
public static void main(String[] args) {
Deque<Integer> tickets = new ArrayDeque<>();
tickets.addLast(101);
tickets.addLast(102);
tickets.addLast(103);
System.out.println(tickets);
System.out.println("Last: " + tickets.getLast());
System.out.println("Next served: " + tickets.removeFirst());
System.out.println(tickets);
}
}
Output
[101, 102, 103]
Last: 103
Next served: 101
[102, 103]
How It Works
Arrival order. Tickets 101, 102, and 103 are each placed behind the previous one.
The newest ticket.getLast() reads the tail, which is 103, without removing it.
Serve the oldest.removeFirst() takes 101 off the head and returns it.
What remains. The deque prints [102, 103], ready for the next customer.
Example 2 — Mix addFirst, addLast, and add
Grow the deque from both sides and check where add() puts its element.
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeAddLastMix {
public static void main(String[] args) {
Deque<String> d = new ArrayDeque<>();
d.addLast("middle");
d.addFirst("left");
d.addLast("right");
System.out.println(d);
d.add("far right");
System.out.println(d);
}
}
Output
[left, middle, right]
[left, middle, right, far right]
How It Works
A starting point."middle" is the only element after the first call.
Grow left and right.addFirst("left") goes before it and addLast("right") after it.
add is addLast.d.add("far right") puts its element at the tail too, behind "right".
Head to tail.println lists elements from head to tail, so the left-to-right picture matches the method names.
Example 3 — Keep the Last Three Readings
A sliding window: add each new reading at the tail and drop the oldest from the head when there are more than three.
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeSlidingWindow {
public static void main(String[] args) {
int[] readings = {4, 8, 15, 16, 23};
Deque<Integer> window = new ArrayDeque<>();
for (int r : readings) {
window.addLast(r);
if (window.size() > 3) {
window.removeFirst();
}
System.out.println(window);
}
}
}
Output
[4]
[4, 8]
[4, 8, 15]
[8, 15, 16]
[15, 16, 23]
How It Works
Fill up. The first three readings simply go to the tail, so the window grows to [4, 8, 15].
Too many. Adding 16 makes four elements, so removeFirst() drops 4, the oldest reading.
Slide again. Adding 23 pushes the size to four once more, and 8 is dropped from the head.
Cheap at both ends. Adding at the tail and removing at the head are both constant-time for an ArrayDeque, which makes it a good fit for windows like this.
Frequently Asked Questions
It inserts the element at the tail (end) of the deque and returns nothing.
They put the element in the same place. add() returns true, while addLast() returns void.
Both insert at the tail. offerLast returns true; addLast returns void. For ArrayDeque neither can fail for lack of space.
No. addLast(null) throws NullPointerException.
removeLast() or pollLast() take it back from the tail. removeFirst() or pollFirst() take the oldest element from the head instead.