java.util.concurrent.ConcurrentLinkedDeque.addFirst()是Java中的內置函數,它將指定的元素插入ConcurrentLinkedDeque的前麵。
用法:
conn_linked_deque.addFirst(elem)
參數:該方法僅接受要添加到ConcurrentLinkedDeque開頭的單個參數elem。
返回值:該函數沒有返回值。
異常:當傳遞給函數的參數為null時,該方法將引發NullPointerException。由於其有限的性質,此方法將永遠不會引發IllegalStateException或返回false。
以下示例程序旨在說明ConcurrentLinkedDeque.addFirst()方法:
示例1:該程序涉及一個Integer類型的ConcurrentLinkedDeque。
// Java Program Demonstrate addFirst()
// method of ConcurrentLinkedDeque
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
public static void main(String[] args)
{
ConcurrentLinkedDeque<Integer> cld =
new ConcurrentLinkedDeque<Integer>();
cld.addFirst(12);
cld.addFirst(110);
cld.addFirst(55);
cld.addFirst(76);
// Displaying the existing LinkedDeque
System.out.println("Initial Elements in"
+ "the LinkedDeque: " + cld);
// Insert a new element in the LinkedDeque
cld.addFirst(21);
// Displaying the modified LinkedDeque
System.out.println("Initial Elements in"
+ "the LinkedDeque: " + cld);
}
}
輸出:
Initial Elements inthe LinkedDeque: [76, 55, 110, 12] Initial Elements inthe LinkedDeque: [21, 76, 55, 110, 12]
示例2:當將null作為參數傳遞給函數時,此程序涉及一個帶有異常處理的Integer類型的ConcurrentLinkedDeque。
// Java Program Demonstrate addFirst()
// method of ConcurrentLinkedDeque
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
public static void main(String[] args)
{
ConcurrentLinkedDeque<String> cld =
new ConcurrentLinkedDeque<String>();
cld.addFirst("Geeks");
cld.addFirst("Geek");
cld.addFirst("Gfg");
cld.addFirst("Contribute");
// Displaying the existing LinkedDeque
System.out.println("Initial Elements in"
+ "the LinkedDeque: " + cld);
/* Exception thrown when null
is passed as parameter*/
try {
cld.addFirst(null);
}
catch (NullPointerException e) {
System.out.println("NullPointerException"
+ "thrown");
}
// Insert a new element in the LinkedDeque
cld.addFirst("Sudo Placement");
// Displaying the modified LinkedDeque
System.out.println("Initial Elements in"
+ "the LinkedDeque: " + cld);
}
}
輸出:
Initial Elements inthe LinkedDeque: [Contribute, Gfg, Geek, Geeks] NullPointerExceptionthrown Initial Elements inthe LinkedDeque: [Sudo Placement, Contribute, Gfg, Geek, Geeks]
參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#addFirst()
相關用法
- Java LinkedList addFirst()用法及代碼示例
- Java ArrayDeque addFirst()用法及代碼示例
- Java LinkedBlockingDeque addFirst()用法及代碼示例
- Java BlockingDeque addFirst()用法及代碼示例
- Java Deque addFirst()用法及代碼示例
- Java ConcurrentLinkedDeque add()用法及代碼示例
- Java ConcurrentLinkedDeque contains()用法及代碼示例
- Java ConcurrentLinkedDeque clear()用法及代碼示例
- Java ConcurrentLinkedDeque getFirst()用法及代碼示例
- Java ConcurrentLinkedDeque iterator()用法及代碼示例
- Java ConcurrentLinkedDeque getLast()用法及代碼示例
- Java ConcurrentLinkedDeque offerFirst()用法及代碼示例
- Java ConcurrentLinkedDeque offerLast()用法及代碼示例
- Java ConcurrentLinkedDeque pollLast()用法及代碼示例
- Java ConcurrentLinkedDeque peek()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 ConcurrentLinkedDeque addFirst() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。