Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 其他

Java API 與框架

Java 類引用

Java 有用資源

Java - Queue offer(E) 方法



描述

Java Queue offer(E e) 方法在不違反容量限制的情況下,如果可以立即將指定的元素插入到此佇列中,則插入該元素。當使用容量受限的佇列時,此方法通常優於 add(E),add(E) 只能透過丟擲異常來失敗插入元素。

宣告

以下是 java.util.Queue.offer() 方法的宣告

public boolean offer(E e)

引數

e − 要新增到末尾的元素。

返回值

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

異常

ClassCastException − 如果指定元素的類阻止它新增到此佇列

NullPointerException − 如果指定元素為空,並且此佇列不允許空元素

IllegalArgumentException − 如果此元素的某些屬性阻止它新增到此佇列

示例 1

以下示例演示了使用整數的 Java Queue offer(E) 方法。我們使用 add() 方法將一些元素新增到列表中,然後使用 offer() 方法在末尾新增兩個元素。最後,我們列印 Queue 物件以檢視最終結果。

package com.tutorialspoint;

import java.util.LinkedList;
import java.util.Queue;

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

      // create an empty queue
      Queue<Integer> queue = new LinkedList<>();

      // use add() method to add elements in the queue
      queue.add(1);
      queue.add(2);
      queue.add(3);
      queue.add(4);        
      queue.add(5);
      queue.add(6);

      // the values will be printed in the same order
      queue.offer(7);
      queue.offer(8);

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

輸出

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

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

示例 2

以下示例演示了使用字串的 Java Queue offer(E) 方法。我們使用 add() 方法將一些元素新增到列表中,然後使用 offer() 方法在末尾新增兩個元素。最後,我們列印 Queue 物件以檢視最終結果。

package com.tutorialspoint;

import java.util.LinkedList;
import java.util.Queue;

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

      // create an empty queue
      Queue<String> queue = new LinkedList<>();

      // use add() method to add elements in the queue
      queue.add("A");
      queue.add("B");
      queue.add("C");
      queue.add("D");        
      queue.add("E");
      queue.add("F");

      // the values will be printed in the same order
      queue.offer("G");
      queue.offer("H");

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

輸出

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

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

示例 3

以下示例演示了使用 Student 物件的 Java Queue offer(E) 方法。我們使用 add() 方法將一些元素新增到列表中,然後使用 offer() 方法在末尾新增兩個元素。最後,我們列印 Queue 物件以檢視最終結果。

package com.tutorialspoint;

import java.util.LinkedList;
import java.util.Queue;

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

      // create an empty queue
      Queue<Student> queue = new LinkedList<>();

      // use add() method to add elements in the queue
      queue.add(new Student(1, "Julie"));
      queue.add(new Student(2, "Robert"));
      queue.add(new Student(3, "Adam"));

      // use offer() method to add element at the end of the queue
      queue.offer(new Student(4, "Rohan"));
      queue.offer(new Student(5, "Sohan"));

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

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 + " ]";
   }
}

輸出

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

Queue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Rohan ], [ 5, Sohan ]]
java_util_linkedlist.htm
廣告