Course
ArrayDeque Class
Java Tutorial
This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.
ArrayDeque Class
Introduction
The Java ArrayDeque class provides resizable-array and implements the Deque interface. Following are the important points about Array Deques
- Array deques have no capacity restrictions so they grow as necessary to support usage.
- Null elements are prohibited in the array deques.
- They are faster than Stack and LinkedList.
This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.
ArrayDeque Class Declaration
Following is the declaration for java.util.ArrayDeque class
public class ArrayDeque<E> extends AbstractCollection<E> implements Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, Queue<E>
Here <E> represents an Element, which could be any class. For example, if you're building an array list of Integers then you'd initialize it as
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayDeque Class Constructors
ArrayDeque Class Methods
ArrayDeque Class Example
// Importing classesimport java.util.ArrayDeque;import java.util.Deque;
// Public Main Classpublic class Main { public static void main(String[] args) { // The main() function Deque < Integer > objDeque = new ArrayDeque < > (); // Adding elements at first and last objDeque.addFirst(15); objDeque.addLast(28);
// Removing the elements int ele1 = objDeque.removeFirst(); int ele2 = objDeque.removeLast();
// Printing removed elements System.out.println("First removed element is : " + ele1); System.out.println("Last removed element is : " + ele2); }}
This will produce the following result
First removed element is : 15Last removed element is : 28
Print