ArrayDeque in Java
- ArrayDeque = Resizable array + Deque interface.
- ArrayDeque implements the Queue & Deque interface.
- There are no capacity restrictions for ArrayDeque, and it provides us the facility to add or remove any element from both sides of the queue.
- Also known as Array Double Ended Queue.
- It is faster than Linked list and stack.
Constructors of ArrayDeque class :
- ArrayDeque(): Used to create an empty array deque that has the capacity to hold 16 elements.
- ArrayDeque(int numElements): Used to create an empty array deque that has the capacity to hold the specified number of elements.
- ArrayDeque(Collection<? extends E> c): Used to create an array deque containing all the elements of the specified collections.
Performing Various Operation On ArrayDeque() :
-
Inserting an element :
-
Insertion at front : add(), offerFirst() and addFirst() methods are used to insert an element at front of an array deque.
Example :
-
Output :
Insertion At End: addLast() and offerLast() methods are used to insert an element at the end of the array deque.Example :
Output :
Accessing an element :
- Accessing an element from the head of the deque array: getFirst() & peekFirst() methods are used to get the first element of the deque array.
Example :
Output :
Accessing the last element: getLast() or peekLast() methods are used to print the last element of the deque array.Example :
Output :
Removing an element :
- Removing the first element: removeFirst() & pollFirst() methods are used to delete an element from the head of the queue.
- removeFirst() throws an exception if the queue is empty.
- pollFirst() returns null if the queue is empty.
Example :
No comments:
Post a Comment