An Deque
interface. ArrayDeque
can be used as both Stack or Queue. Null
elements are prohibited.
removeFirst
methodThe removeLast
method gets and removes ArrayDeque
object’s last element.
public E removeLast()
This method doesn’t take any parameters.
This method retrieves and removes the last element of the Deque
object. If the Deque
object is empty then NoSuchElementException
is thrown.
This method is the same as
pollLast
method except thepollLast
method doesn’t throwNoSuchElementException
if thedeque
is empty.
The code below demonstrates the use of the removeLast
method.
import java.util.ArrayDeque;class RemoveLast {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.removeLast() returns : " + arrayDeque.removeLast());System.out.println("The arrayDeque is " + arrayDeque);}}
In the code above:
In line 1, we import the ArrayDeque
class.
In line 4, we create a ArrayDeque
object named arrayDeque
.
From line 5 to 7, we use arrayDeque
object’s add method to add three elements("1"
,"2"
,"3"
) to Deque
.
In line 10, we use the removeLast()
method of the arrayDeque
object to get and remove the last element. In our case, 3
will be removed and returned.