0
0
JavaHow-ToBeginner · 4 min read

How to Use ArrayDeque in Java: Syntax and Examples

In Java, ArrayDeque is a resizable array that implements the Deque interface, allowing you to add, remove, and access elements from both ends efficiently. You create an ArrayDeque object and use methods like addFirst(), addLast(), removeFirst(), and removeLast() to work with it.
📐

Syntax

The basic syntax to use ArrayDeque involves importing it, creating an instance, and then using its methods to add or remove elements from either end.

  • Import: import java.util.ArrayDeque;
  • Create: ArrayDeque<Type> deque = new ArrayDeque<>();
  • Add elements: addFirst() adds to front, addLast() adds to end
  • Remove elements: removeFirst() removes from front, removeLast() removes from end
java
import java.util.ArrayDeque;

public class Example {
    public static void main(String[] args) {
        ArrayDeque<String> deque = new ArrayDeque<>();
        deque.addFirst("front");
        deque.addLast("end");
        System.out.println(deque);
    }
}
Output
[front, end]
💻

Example

This example shows how to create an ArrayDeque, add elements to both ends, remove elements, and peek at elements without removing them.

java
import java.util.ArrayDeque;

public class ArrayDequeExample {
    public static void main(String[] args) {
        ArrayDeque<Integer> deque = new ArrayDeque<>();

        // Add elements at the end
        deque.addLast(10);
        deque.addLast(20);

        // Add element at the front
        deque.addFirst(5);

        System.out.println("Deque after additions: " + deque);

        // Peek at first and last elements
        System.out.println("First element: " + deque.peekFirst());
        System.out.println("Last element: " + deque.peekLast());

        // Remove elements from both ends
        int removedFirst = deque.removeFirst();
        int removedLast = deque.removeLast();

        System.out.println("Removed first: " + removedFirst);
        System.out.println("Removed last: " + removedLast);
        System.out.println("Deque after removals: " + deque);
    }
}
Output
Deque after additions: [5, 10, 20] First element: 5 Last element: 20 Removed first: 5 Removed last: 20 Deque after removals: [10]
⚠️

Common Pitfalls

Common mistakes when using ArrayDeque include:

  • Using null elements, which ArrayDeque does not allow and throws NullPointerException.
  • Confusing add() and offer() methods; add() throws exception if full, offer() returns false.
  • Using pop() and push() methods without understanding they treat the deque like a stack (LIFO).

Example of wrong and right usage:

java
import java.util.ArrayDeque;

public class PitfallExample {
    public static void main(String[] args) {
        ArrayDeque<String> deque = new ArrayDeque<>();

        // Wrong: adding null causes NullPointerException
        // deque.add(null); // Uncommenting this line will throw exception

        // Right: add non-null elements
        deque.add("hello");
        deque.addFirst("start");

        System.out.println(deque);
    }
}
Output
[start, hello]
📊

Quick Reference

MethodDescription
addFirst(E e)Adds element at the front
addLast(E e)Adds element at the end
removeFirst()Removes and returns first element
removeLast()Removes and returns last element
peekFirst()Returns first element without removing
peekLast()Returns last element without removing
push(E e)Adds element at front (stack behavior)
pop()Removes and returns first element (stack behavior)

Key Takeaways

ArrayDeque allows fast adding/removing from both ends without null elements.
Use addFirst/addLast to add and removeFirst/removeLast to remove elements.
Avoid adding null values to prevent exceptions.
peekFirst and peekLast let you see elements without removing them.
ArrayDeque can be used as a stack with push and pop methods.