ArrayDeque in Java

An ArrayDeque in Java is an implementation of the Deque interface that is backed by an array. It is similar to a regular array but it allows for dynamic resizing and provides methods for adding and removing elements from both the front and back of the array. In this article, we will discuss what an ArrayDeque is, how to create one in Java, and how to use its methods to manipulate the array.

What is an ArrayDeque?


An ArrayDeque is a data structure that implements the Deque interface and is backed by an array. It allows for dynamic resizing, which means that the array can grow or shrink in size as elements are added or removed. Unlike regular arrays, an ArrayDeque allows for efficient addition and removal of elements from both the front and back of the array.

Creating an ArrayDeque in Java


To create an ArrayDeque in Java, we can use the ArrayDeque class which is part of the Java Collections framework. Here’s an example of how to create an ArrayDeque that can store integers:

Deque deque = new ArrayDeque();


In this example, we create an ArrayDeque object that can store integers. We use the Deque interface as the type of the object to take advantage of the polymorphism that it provides. The new operator is used to create a new ArrayDeque object, and we assign it to the deque variable.

Adding Elements to an ArrayDeque


To add elements to the ArrayDeque, we can use the addFirst(), addLast(), offerFirst(), and offerLast() methods. The addFirst() and addLast() methods add the specified element to the front and back of the deque respectively. The offerFirst() and offerLast() methods are similar to the add methods but they return a boolean value indicating whether the addition was successful or not. Here’s an example of how to add elements to an ArrayDeque:

deque.addFirst(1);
deque.addLast(2);
deque.offerFirst(3);
deque.offerLast(4);

In this example, we add four elements to the ArrayDeque. The first element is added to the front of the deque using the addFirst() method. The second element is added to the back of the deque using the addLast() method. The third element is added to the front of the deque using the offerFirst() method. The fourth element is added to the back of the deque using the offerLast() method.

Removing Elements from an ArrayDeque


To remove elements from the ArrayDeque, we can use the removeFirst(), removeLast(), pollFirst(), and pollLast() methods. The removeFirst() and removeLast() methods remove and return the first and last elements of the deque respectively. The pollFirst() and pollLast() methods are similar to the remove methods but they return null if the deque is empty. Here’s an example of how to remove elements from an ArrayDeque:

deque.removeFirst();
deque.removeLast();
deque.pollFirst();
deque.pollLast();


In this example, we remove four elements from the ArrayDeque. The first element is removed from the front of the deque using the removeFirst() method. The second element is removed from the back of the deque using the removeLast() method. The third element is removed from the front of the deque using the pollFirst() method. The fourth element is removed from the back of the deque using the pollLast() method.

Accessing Elements in an ArrayDeque


To access elements in the ArrayDeque, we can use the getFirst(), getLast(), peekFirst(), and peekLast() methods. The getFirst() and getLast() methods return the first and last elements of the deque respectively. The peekFirst() and peekLast() methods are similar to the get methods but they return null if the deque is empty. Here’s an example of how to access elements in an ArrayDeque:

deque.addFirst(1);
deque.addLast(2);
System.out.println(deque.getFirst()); // prints 1
System.out.println(deque.getLast()); // prints 2
System.out.println(deque.peekFirst()); // prints 1
System.out.println(deque.peekLast()); // prints 2

In this example, we add two elements to the ArrayDeque. We then use the getFirst() and getLast() methods to print the first and last elements of the deque. We also use the peekFirst() and peekLast() methods to print the first and last elements of the deque without removing them.

Iterating Over an ArrayDeque


To iterate over the elements of the ArrayDeque, we can use a for-each loop or an iterator. Here’s an example of how to iterate over an ArrayDeque using a for-each loop:

Deque deque = new ArrayDeque();
deque.addFirst("John");
deque.addLast("Jane");
deque.addLast("Mary");

for(String name : deque) {
System.out.println(name);
}


In this example, we create an ArrayDeque object that can store strings. We add three elements to the deque using the addFirst() and addLast() methods. We then use a for-each loop to iterate over the elements of the deque and print them to the console.

Conclusion


In summary, an ArrayDeque in Java is a data structure that implements the Deque interface and is backed by an array. It allows for efficient addition and removal of elements from both the front and back of the array. To create an ArrayDeque, we can use the ArrayDeque class and add and remove elements using the add, remove, offer, and poll methods. We can access elements in the ArrayDeque using the get and peek methods and iterate over the elements using a for-each loop or an iterator. ArrayDeque is a versatile data structure that can be used in a variety of applications where dynamic resizing and efficient addition and removal of elements are required.