w3resource

Java ArrayDeque Class: push() Method

public void push(E e)

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

This method is equivalent to addFirst(E).

Package: java.util

Java Platform: Java SE 8

Syntax:

push(E e)

Parameters:

Name Description
e the element to push

Return Value Type: E

Throws:

NullPointerException - if the specified element is null

Pictorial Presentation

Java ArrayDeque Class: push() Method

Example: Java ArrayDeque Class: push() 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);
      }

     // Add two elements using push() method
      deque.push(1000);
      deque.push(1500);

      // Print all the elements available in deque after pushing two elements using push()
      System.out.println("New deque:");
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }
   }
}

Output:

Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
New deque:
Number = 1500
Number = 1000
Number = 100
Number = 200
Number = 150
Number = 95

Java Code Editor:

Previous:pop Method
Next:remove Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_push.php