Java ArrayDeque push() 方法



描述

Java ArrayDeque push(E e) 方法將元素 E 推送到由該 deque 表示的棧中。本質上,它將元素新增到 ArrayDeque 物件的頭部。

宣告

以下是 java.util.ArrayDeque.push() 方法的宣告

public void push(E e)

引數

e − 要推入 deque 的元素。

返回值

此方法不返回值。

異常

NullPointerException − 如果指定的元素為 null

將元素推入整數型 ArrayDeque 的示例

以下示例演示了 Java ArrayDeque push(E) 方法的使用。在此示例中,我們使用整數。首先,我們將使用 add() 方法向 deque 新增一些項,然後使用 push() 方法將元素新增到棧中。然後,我們再次使用 add() 方法新增更多元素,並列印 arraydeque 以檢查 deque 中的插入是否按我們期望的順序進行。

package com.tutorialspoint;

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

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

      // use add() method to add elements in the deque
      deque.add(4);
      deque.add(5);
      deque.add(6);

      // use push() method to add element at the front of the deque
      deque.push(3);
      deque.push(2);
      deque.push(1);//now, element 1 will be at the front

      // these elements will be added in continuation with deque.add(6)
      deque.add(7);
      deque.add(8);

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

ArrayDeque = [1, 2, 3, 4, 5, 6, 7, 8]

將元素推入字串型 ArrayDeque 的示例

以下示例演示了 Java ArrayDeque push(E) 方法的使用。在此示例中,我們使用字串。首先,我們將使用 add() 方法向 deque 新增一些項,然後使用 push() 方法將元素新增到棧中。然後,我們再次使用 add() 方法新增更多元素,並列印 arraydeque 以檢查 deque 中的插入是否按我們期望的順序進行。

package com.tutorialspoint;

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

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      Deque<String> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add("D");
      deque.add("E");
      deque.add("F");

      // use push() method to add element to the stack
      deque.push("C");
      deque.push("B");
      deque.push("A");//now, element A will be at the front

      // these elements will be added in continuation with deque.add("F")
      deque.add("G");
      deque.add("H");

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

ArrayDeque = [A, B, C, D, E, F, G, H]

將元素推入物件型 ArrayDeque 的示例

以下示例演示了 Java ArrayDeque push(E) 方法的使用。在此示例中,我們使用 Student 物件。首先,我們將使用 add() 方法向 deque 新增一些項,然後使用 push() 方法將元素新增到 deque 的頭部。然後,我們再次使用 add() 方法新增更多元素,並列印 arraydeque 以檢查 deque 中的插入是否按我們期望的順序進行。

package com.tutorialspoint;

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

public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<Student> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(new Student(4, "Julie"));
      deque.add(new Student(5, "Robert"));
      deque.add(new Student(6, "Adam"));

      // use push() method to add element to the stack
      deque.push(new Student(3, "Rohan"));
      deque.push(new Student(2, "Sohan"));
      deque.push(new Student(1, "Mohan"));//now, Student 1 will be at the front

      // these elements will be added in continuation with deque.add(new Student(6, "Adam"))
      deque.add(new Student(7, "Ali"));
      deque.add(new Student(8, "Ahmad"));

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);      
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

ArrayDeque = [[ 1, Mohan ], [ 2, Sohan ], [ 3, Rohan ], [ 4, Julie ], [ 5, Robert ], [ 6, Adam ], [ 7, Ali ], [ 8, Ahmad ]]
java_util_arraydeque.htm
廣告