如果有空間,LinkedBlockingDeque的addLast(E e)方法會將參數中傳遞的元素插入到Deque的末尾。如果LinkedBlockingDeque受容量限製並且沒有剩餘空間可插入,則它返回IllegalStateException。
用法:
public void addLast(E e)
參數:此方法接受強製參數e,該參數是要在LinkedBlockingDeque末尾插入的元素。
返回值:此方法不返回任何內容。
異常:
- IllegalStateException:如果由於容量限製此時無法添加元素
- NullPointerException :如果指定的元素為null
以下示例程序旨在說明LinkedBlockingDeque的addLast()方法:
示例1:
// Java Program Demonstrate addLast()
// method of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of LinkedBlockingDeque
LBD.addLast(7855642);
LBD.addLast(35658786);
LBD.addLast(5278367);
LBD.addLast(74381793);
// before removing print queue
System.out.println("Linked Blocking Deque: " + LBD);
}
}
輸出:
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
示例2:
// Java Program Demonstrate addLast()
// method of LinkedBlockingDeque
// when it is Full
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
// size of list
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>(3);
// Add numbers to end of LinkedBlockingDeque
LBD.addLast(7855642);
LBD.addLast(35658786);
LBD.addLast(5278367);
// it is full
LBD.addLast(74381793);
// before removing print queue
System.out.println("Linked Blocking Deque: " + LBD);
}
}
輸出:
Exception in thread "main" java.lang.IllegalStateException: Deque full at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:335) at GFG.main(GFG.java:23)
示例3:
// Java Program Demonstrate addLast()
// method of LinkedBlockingDeque
// when nill is inserted
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of LinkedBlockingDeque
LBD.addLast(7855642);
LBD.addLast(35658786);
LBD.addLast(5278367);
// NULL
LBD.addLast(null);
// before removing print queue
System.out.println("Linked Blocking Deque: " + LBD);
}
}
輸出:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357) at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:334) at GFG.main(GFG.java:23)
參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#addLast(E)
相關用法
- Java Deque addLast()用法及代碼示例
- Java LinkedList addLast()用法及代碼示例
- Java ArrayDeque addLast()用法及代碼示例
- Java ConcurrentLinkedDeque addLast()用法及代碼示例
- Java BlockingDeque addLast()用法及代碼示例
- Java LinkedBlockingDeque put()用法及代碼示例
- Java LinkedBlockingDeque pop()用法及代碼示例
- Java LinkedBlockingDeque contains()用法及代碼示例
- Java LinkedBlockingDeque take()用法及代碼示例
- Java LinkedBlockingDeque add()用法及代碼示例
- Java LinkedBlockingDeque peekFirst()用法及代碼示例
- Java LinkedBlockingDeque pollFirst()用法及代碼示例
- Java LinkedBlockingDeque pollLast()用法及代碼示例
- Java LinkedBlockingDeque addFirst()用法及代碼示例
- Java LinkedBlockingDeque descendingIterator()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LinkedBlockingDeque addLast() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。