A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_pop.php below:

Java ArrayDeque Class: pop() Method

Java ArrayDeque Class: pop() MethodLast update on August 19 2022 21:51:24 (UTC/GMT +8 hours) public E pop()

The pop() method is used to pop an element from the stack represented by a given deque.

This method is equivalent to removeFirst().

Package: java.util

Java Platform: Java SE 8

Syntax:

pop()

Return Value:

the element at the front of this deque (which is the top of the stack represented by this deque)

Return Value Type: E - the type of elements held in this collection

Throws:

NoSuchElementException - if this deque is empty

Pictorial Presentation

Example: Java ArrayDeque Class: pop() Method

import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
   public static void main(String[] args) {
      
      // Create an array deque 
      Deque<Integer> deque = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque.add(100);
      deque.add(200);
      deque.add(150);
      deque.add(95);        

      // Print all the elements of the original deque
      System.out.println("Elements of the original deque:");
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }

      int rval = deque.pop();
      System.out.println("Removed element: " + rval);

      // printing all the elements available in deque after using pop()
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }
   }
}

Output:

Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
Removed element: 100
Number = 200
Number = 150
Number = 95

Java Code Editor:

Previous:pollLast Method
Next:push Method




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4