當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Queue offer()用法及代碼示例


隊列接口的offer(E e)方法在不違反容量限製的情況下可以立即將指定的元素插入此隊列。此方法優於add()方法,因為在容器的容量已滿時,此方法不會引發異常,因為它會返回false。

用法:

boolean offer(E e)

參數:此方法接受強製參數e,該參數是要插入隊列中的元素。

返回:成功插入時此方法返回true,否則返回false。

異常:該函數引發四個異常,如下所述:



  • ClassCastException:當要輸入的元素的類阻止將其添加到此容器中時:
  • IllegalArgumentException:當元素的某些屬性阻止將其添加到隊列中時:
  • NullPointerException :當要插入的元素作為null傳遞並且Queue的接口不允許使用null元素時。

以下示例程序旨在說明offer()隊列方法:

程序1:借助LinkedBlockingDeque。

// Java Program Demonstrate offer() 
// method of Queue 
  
import java.util.*; 
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of Queue 
        Queue<Integer> Q 
            = new LinkedBlockingQueue<Integer>(3); 
  
        if (Q.offer(10)) 
            System.out.println("The Queue is not full"
                               + " and 10 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(15)) 
            System.out.println("The Queue is not full"
                               + " and 15 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(25)) 
            System.out.println("The Queue is not full"
                               + " and 25 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(20)) 
            System.out.println("The Queue is not full"
                               + " and 20 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        // before removing print Queue 
        System.out.println("Queue:" + Q); 
    } 
}
輸出:
The Queue is not full and 10 is inserted
The Queue is not full and 15 is inserted
The Queue is not full and 25 is inserted
The Queue is full
Queue:[10, 15, 25]

程序2:借助ConcurrentLinkedDeque。

// Java Program Demonstrate offer() 
// method of Queue 
  
import java.util.*; 
import java.util.concurrent.ConcurrentLinkedDeque; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of Queue 
        Queue<Integer> Q 
            = new ConcurrentLinkedDeque<Integer>(); 
  
        if (Q.offer(10)) 
            System.out.println("The Queue is not full"
                               + " and 10 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(15)) 
            System.out.println("The Queue is not full"
                               + " and 15 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(25)) 
            System.out.println("The Queue is not full"
                               + " and 25 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(20)) 
            System.out.println("The Queue is not full"
                               + " and 20 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        // before removing print Queue 
        System.out.println("Queue:" + Q); 
    } 
}
輸出:
The Queue is not full and 10 is inserted
The Queue is not full and 15 is inserted
The Queue is not full and 25 is inserted
The Queue is not full and 20 is inserted
Queue:[10, 15, 25, 20]

程序3:借助ArrayDeque。

// Java Program Demonstrate offer() 
// method of Queue 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of Queue 
        Queue<Integer> Q 
            = new ArrayDeque<Integer>(6); 
  
        if (Q.offer(10)) 
            System.out.println("The Queue is not full"
                               + " and 10 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(15)) 
            System.out.println("The Queue is not full"
                               + " and 15 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(25)) 
            System.out.println("The Queue is not full"
                               + " and 25 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(20)) 
            System.out.println("The Queue is not full"
                               + " and 20 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        // before removing print Queue 
        System.out.println("Queue:" + Q); 
    } 
}
輸出:
The Queue is not full and 10 is inserted
The Queue is not full and 15 is inserted
The Queue is not full and 25 is inserted
The Queue is not full and 20 is inserted
Queue:[10, 15, 25, 20]

程序4:借助LinkedList。



// Java Program Demonstrate offer() 
// method of Queue 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of Queue 
        Queue<Integer> Q 
            = new LinkedList<Integer>(); 
  
        if (Q.offer(10)) 
            System.out.println("The Queue is not full"
                               + " and 10 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(15)) 
            System.out.println("The Queue is not full"
                               + " and 15 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(25)) 
            System.out.println("The Queue is not full"
                               + " and 25 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        if (Q.offer(20)) 
            System.out.println("The Queue is not full"
                               + " and 20 is inserted"); 
        else
            System.out.println("The Queue is full"); 
  
        // before removing print Queue 
        System.out.println("Queue:" + Q); 
    } 
}
輸出:
The Queue is not full and 10 is inserted
The Queue is not full and 15 is inserted
The Queue is not full and 25 is inserted
The Queue is not full and 20 is inserted
Queue:[10, 15, 25, 20]

以下示例程序旨在說明此方法引發的異常:

程序5:顯示NullPointerException。

// Java Program Demonstrate offer() 
// method of Queue when Null is passed 
  
import java.util.*; 
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
    public static void main(String[] args) 
        throws NullPointerException 
    { 
  
        // create object of Queue 
        Queue<Integer> Q 
            = new LinkedBlockingQueue<Integer>(); 
  
        // Add numbers to end of Deque 
        Q.offer(7855642); 
        Q.offer(35658786); 
        Q.offer(5278367); 
  
        try { 
            // when null is inserted 
            Q.offer(null); 
        } 
        catch (Exception e) { 
            System.out.println("Exception:" + e); 
        } 
    } 
}
輸出:
Exception:java.lang.NullPointerException

注意:其他兩個異常是內部的,它們是由編譯器引起的,因此無法在代碼中顯示。

參考: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#offer-E-

相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Queue offer() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。