ArrayDeque pop() Method in Java
The Java.util.ArrayDeque.pop() method retrieves and pops an element from the deque. The pop() method in Java is intended to replicate the functionality of a stack. It effectively removes the element at the top of the deque. Invoking this method not only retrieves the element but also permanently discards it from the deque and changes the deque state accordingly.
Syntax:
DequeArray.pop()
Parameters: The method does not accept any parameters.
Return Value: The method returns the element at the front of the deque.
Exceptions: If the deque is empty, the method throws NoSuchElementException.
Example 1:
Here is an example of the pop() method in the Java ArrayDeque class. Initially, an empty ArrayDeque called dequeArr is created. The strings such as Welcome, To, Free, Online, and Tutorials are then added to dequeArr. The initial elements of dequeArr are displayed. The pop() method is then used twice to remove elements from the ArrayDeque, and the resulting elements are printed. Finally, the elements of dequeArr after the pop() operation are displayed.
FileName: ArrDequeExample.java
// importing util packages import java.util.*; public class ArrDequeExample { public static void main(String args[]) { // creating an empty dequeArr DequedequeArr = new ArrayDeque (); // adding elements into the Deque using the add() method dequeArr.add("Welcome"); dequeArr.add("To"); dequeArr.add("Free"); dequeArr.add("Online"); dequeArr.add("Tutorials"); // displaying the initial ArrayDeque System.out.println("Initial ArrayDeque is : " + dequeArr); // using pop() method elements are discarded System.out.println("The popped element is: " + dequeArr.pop()); System.out.println("The popped element is: " + dequeArr.pop()); // displaying the ArrayDeque after performing the pop operation System.out.println("Deque after performing pop operation" + dequeArr); } }
Output:
Initial ArrayDeque is : [Welcome, To, Free, Online, Tutorials] The popped element is: Welcome The popped element is: To Deque after performing pop operation [Free, Online, Tutorials]
Example 2:
The example below illustrates the usage pop() method in Java ArrayDeque class. Firstly, an empty ArrayDeque called dequeArr is created. The integers are then added to dequeArr. The initial integer elements of dequeArr are displayed. The pop() method is then used twice to pop elements from the ArrayDeque, and the resulting elements are printed. Finally, the elements of dequeArr after the pop() operation are displayed.
FileName: ArrDequeExample.java
// importing util packages import java.util.*; public class ArrDequeExample { public static void main(String args[]) { // creating an empty dequeArr DequedequeArr = new ArrayDeque (); // adding elements into the Deque using add() method dequeArr.add(13); dequeArr.add(16); dequeArr.add(30); dequeArr.add(26); dequeArr.add(4); dequeArr.add(32); // displaying the initial ArrayDeque System.out.println("Initial ArrayDeque: " + dequeArr); // using pop() method elements are discarded System.out.println("The popped element is: " + dequeArr.pop()); System.out.println("The popped element is: " + dequeArr.pop()); // displaying the ArrayDeque after performing pop operation System.out.println("Deque after performing pop operation" + dequeArr); } }
Output:]
Initial ArrayDeque: [13, 16, 30, 26, 4, 32] The popped element is: 13 The popped element is: 16 Deque after performing pop operation [30, 26, 4, 32]