LinkedBlockingDeque的getLast()方法返回Deque容器中的最後一個元素。如果LinkedBlockingDeque為空,則在函數調用時將返回NoSuchElementException。
用法:
public E getLast()
參數:此方法不接受任何參數。
返回值:此方法返回此雙端隊列容器的最後一個元素。
異常:當雙端隊列為空時,該函數僅引發一個異常,即NoSuchElementException
以下示例程序旨在說明LinkedBlockingDeque的getLast()方法:
示例1:
// Java Program Demonstrate getLast()
// 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 front of LinkedBlockingDeque
LBD.addFirst(7855642);
LBD.addFirst(35658786);
LBD.addFirst(5278367);
LBD.addFirst(74381793);
// before removing print queue
System.out.println("Linked Blocking Deque: " + LBD);
// prints the last element
System.out.println("Linked Blocking Deque first element: " +
LBD.getLast());
}
}
輸出:
Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642] Linked Blocking Deque first element: 7855642
示例2:
// Java Program Demonstrate getLast()
// method of LinkedBlockingDeque
// shows exception
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 front of LinkedBlockingDeque
LBD.addFirst(7855642);
LBD.addFirst(35658786);
LBD.addFirst(5278367);
LBD.addFirst(74381793)
LBD.clear();
// Since the container is empty it will throw exception
System.out.println("Linked Blocking Deque first element: " +
LBD.getLast());
}
}
輸出:
Exception in thread "main" java.util.NoSuchElementException at java.util.concurrent.LinkedBlockingDeque.getLast(LinkedBlockingDeque.java:553) at GFG.main(GFG.java:28)
參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#getLast()
相關用法
- Java LinkedList getLast()用法及代碼示例
- Java Deque getLast()用法及代碼示例
- Java ConcurrentLinkedDeque getLast()用法及代碼示例
- Java ArrayDeque getLast()用法及代碼示例
- Java LinkedBlockingDeque contains()用法及代碼示例
- Java LinkedBlockingDeque put()用法及代碼示例
- Java LinkedBlockingDeque add()用法及代碼示例
- Java LinkedBlockingDeque pop()用法及代碼示例
- Java LinkedBlockingDeque take()用法及代碼示例
- Java LinkedBlockingDeque poll()用法及代碼示例
- Java LinkedBlockingDeque peekLast()用法及代碼示例
- Java LinkedBlockingDeque size()用法及代碼示例
- Java LinkedBlockingDeque removeLastOccurrence()用法及代碼示例
- Java LinkedBlockingDeque toString()用法及代碼示例
- Java LinkedBlockingDeque pollFirst()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LinkedBlockingDeque getLast() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。