Java ArrayDeque offerFirst(E) 方法



描述

Java ArrayDeque offerFirst(E e) 方法將指定的元素 E 插入到此雙端佇列的前面。它類似於 addFirst() 方法。

宣告

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

public boolean offerFirst(E e)

引數

e − 要新增到前面的元素。

返回值

如果元素已新增到此雙端佇列,則此方法返回 true,否則返回 false

異常

NullPointerException − 如果指定的元素為 null

將元素新增到整數 ArrayDeque 的前面示例

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

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 offerFirst() method to add element at the front of the deque
      deque.offerFirst(3);
      deque.offerFirst(2);
      deque.offerFirst(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 offerFirst(E) 方法的用法。在此示例中,我們使用字串。首先,我們將使用 add() 方法向雙端佇列新增一些項,然後使用 offerFirst() 方法將元素新增到雙端佇列的前面。然後我們再次使用 add() 方法新增更多元素,並列印陣列以檢查雙端佇列中的插入是否按我們期望的順序進行。

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 offerFirst() method to add element at the front of the deque
      deque.offerFirst("C");
      deque.offerFirst("B");
      deque.offerFirst("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 offerFirst(E) 方法的用法。在此示例中,我們使用 Student 物件。首先,我們將使用 add() 方法向雙端佇列新增一些項,然後使用 offerFirst() 方法將元素新增到雙端佇列的前面。然後我們再次使用 add() 方法新增更多元素,並列印陣列以檢查雙端佇列中的插入是否按我們期望的順序進行。

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 offerFirst() method to add element at the front of the deque
      deque.offerFirst(new Student(3, "Rohan"));
      deque.offerFirst(new Student(2, "Sohan"));
      deque.offerFirst(new Student(1, "Mohan"));//now, Student 1 will be at the front

      // these elments 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
廣告