What is the ArrayDeque.pop method in Java?
An
This class is the implementation class of the Deque interface. ArrayDeque can be used as a stack or queue. Null elements are prohibited.
What is the pop method of ArrayDeque?
The pop method can be used to get and remove the first element of the ArrayDeque object.
Syntax
public E pop()
Parameters
This method doesn’t take any parameters.
Return value
The pop() method retrieves and removes the first element of the deque object. If the deque object is empty, then NoSuchElementException is thrown.
This method is the same as the
removeFirstmethod.
Code
The code below demonstrates how to use the pop method.
import java.util.ArrayDeque;class Pop {public static void main( String args[] ) {ArrayDeque<String> arrayDeque = new ArrayDeque<>();arrayDeque.add("1");arrayDeque.add("2");arrayDeque.add("3");System.out.println("The arrayDeque is " + arrayDeque);System.out.println("arrayDeque.pop() returns : " + arrayDeque.pop());System.out.println("The arrayDeque is " + arrayDeque);}}
Explanation
In the above code:
-
In line 1: We import the
ArrayDequeclass. -
In line 4: We create an
ArrayDequeobject with the namearrayDeque. -
From lines 5 to 7: We use the
add()method of thearrayDequeobject to add three elements ("1","2","3") todequeu. -
In line 10: We use the
pop()method of thearrayDequeobject to get and remove the first element. In our case,1will be removed and returned.