java.util.concurrent.ConcurrentLinkedDeque.offerLast()方法是Java中的內置方法,它將作為參數傳遞的指定元素插入雙端隊列的末尾。
用法:
Conn_Linked_Deque.offerLast(Object elem)
參數:該方法接受參數elem,以選擇要插入雙端隊列的元素的元素。
返回值:如果將元素成功添加到雙端隊列中,則該函數返回True,否則返回False。
異常:如果傳遞的參數為NULL,則該函數將引發NullPointerException。
以下示例程序旨在說明ConcurrentLinkedDeque.offerLast()方法:
程序1:
/* Java Program to Demonstrate offerLast()
method of ConcurrentLinkedDeque */
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
// Creating an empty Deque
ConcurrentLinkedDeque<String> cld =
new ConcurrentLinkedDeque<String>();
// Add elements into the Deque
cld.add("Welcome");
cld.add("To");
cld.add("Geeks");
cld.add("4");
cld.add("Geeks");
// Displaying the Deque
System.out.println("Elements in Deque: "
+ cld);
// Displaying the Last element
System.out.println("The Last element is: " +
cld.getLast());
/* Insert an element at the tail
of the deque */
if (cld.offerLast("GFG")) {
// Displaying the Last element
System.out.println("The Inserted element is: " +
cld.getLast());
}
// Displaying the Deque
System.out.println("Elements in Deque: "
+ cld);
// Displaying the Last element
System.out.println("The Last element is: " +
cld.getLast());
}
}
輸出:
Elements in Deque: [Welcome, To, Geeks, 4, Geeks] The Last element is: Geeks The Inserted element is: GFG Elements in Deque: [Welcome, To, Geeks, 4, Geeks, GFG] The Last element is: GFG
程序2:
/* Java Program to Demonstrate offerLast()
method of ConcurrentLinkedDeque */
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
// Creating an empty Deque
ConcurrentLinkedDeque<Integer> cld =
new ConcurrentLinkedDeque<Integer>();
// Add elements into the Deque
cld.add(12);
cld.add(43);
cld.add(29);
cld.add(16);
cld.add(70);
// Displaying the Deque
System.out.println("Elements in Deque: "
+ cld);
// Displaying the Last element
System.out.println("The Last element is: " +
cld.getLast());
try {
cld.offerLast(null);
}
catch (Exception e) {
System.out.println(e);
}
/* Insert an element at the tail
of the deque */
if (cld.offerLast(24)) {
// Displaying the Last element
System.out.println("The Inserted element is: "
+ cld.getLast());
}
// Displaying the Deque
System.out.println("Elements in Deque: "
+ cld);
// Displaying the Last element
System.out.println("The Last element is: " +
cld.getLast());
}
}
輸出:
Elements in Deque: [12, 43, 29, 16, 70] The Last element is: 70 java.lang.NullPointerException The Inserted element is: 24 Elements in Deque: [12, 43, 29, 16, 70, 24] The Last element is: 24
相關用法
- Java LinkedBlockingDeque offerLast()用法及代碼示例
- Java ArrayDeque offerLast()用法及代碼示例
- Java Deque offerLast()用法及代碼示例
- Java BlockingDeque offerLast()用法及代碼示例
- Java ConcurrentLinkedDeque add()用法及代碼示例
- Java ConcurrentLinkedDeque getLast()用法及代碼示例
- Java ConcurrentLinkedDeque peekFirst()用法及代碼示例
- Java ConcurrentLinkedDeque removeFirst()用法及代碼示例
- Java ConcurrentLinkedDeque descendingIterator()用法及代碼示例
- Java ConcurrentLinkedDeque pollLast()用法及代碼示例
- Java ConcurrentLinkedDeque pop()用法及代碼示例
- Java ConcurrentLinkedDeque pollFirst()用法及代碼示例
- Java ConcurrentLinkedDeque offerFirst()用法及代碼示例
- Java ConcurrentLinkedDeque toArray()用法及代碼示例
- Java ConcurrentLinkedDeque addLast()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 ConcurrentLinkedDeque offerLast() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。