Java Program to Implement ArrayDeque
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
ArrayDeque is a data structure in Java that allows efficient element manipulation operations. It’s a double-ended queue that enables adding and removing elements from both ends of the queue. ArrayDeque in Java is commonly used for implementing data structures such as queues (first-in, first-out), stacks (last-in, first-out), and double-ended queues.
How to create a Deque in Java?
To create a Deque in Java, you can use the ArrayDeque class, which implements the Deque interface. Here’s an example:
This creates an empty Deque that can hold Integer values.
1. Add Element to the Deque
To add an element to the end of the deque, you can use the addLast or offerLast method:
Both methods add element 42 to the end of the deque. The difference is that addLast throws an exception if the deque is full, while offerLast returns false if the deque is full (in this case, the element is not added).
We can also add elements from the back of the deque using the addFirst and offerFirst methods.
2. Remove Element from Deque
To remove an element from the front of the deque, you can use the removeFirst or pollFirst method:
Both methods remove and return the first element of the deque. The difference is that removeFirst throws an exception if the deque is empty, while pollFirst returns null if the deque is empty (in this case, no element is removed).
We can also remove elements from the back of the deque using the removeLast, and pollLast methods.
Write a Java Program to implement Array Deque.
Here is the source code of the Java Program to implement Array Deque. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
1. The program starts by defining the ArrayDeque class, which contains an integer array a, an integer j, and an integer n.
2. The a array stores the elements in the deque, j is an index variable that tracks the position of the front element, and n is the number of elements in the deque.
3. The resize() method is a private method that is called whenever the deque is full, and it doubles the size of the array a to make room for more elements.
4. The program defines several methods for performing operations on the ArrayDeque.
5. The ArrayDequeTest class is the driver class that provides a menu for testing the ArrayDeque methods. It prompts the user to enter the index and value of the element to be added or set, or the index of the element to be removed or retrieved.
6. It also provides options to check whether the deque is empty, clear the deque, and display the size of the deque.
7. The display() method is used to print the elements of the deque to the console.
Consumer interest
Consumer behavior