Java ArrayDeque - removeLast() Method
The java.util.ArrayDeque.removeLast() method is used to remove and return the last element of the deque. Every removal of element results into reducing the deque size by one unless the deque is empty.
Syntax
public E removeLast()
Here, E is the type of element maintained by the container.
Parameters
No parameter is required.
Return Value
Returns the last element of the deque.
Exception
Throws NoSuchElementException, if the deque is empty.
Example:
In the example below, the java.util.ArrayDeque.removeLast() method is used to remove and return the last element of the given deque.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating an ArrayDeque ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>(); //populating the ArrayDeque MyDeque.add(10); MyDeque.add(20); MyDeque.add(30); //printing the ArrayDeque in reverse order System.out.print("MyDeque contains: "); while(MyDeque.size() != 0) { System.out.print(MyDeque.removeLast()+ " "); } } }
The output of the above code will be:
MyDeque contains: 30 20 10
❮ Java.util - ArrayDeque