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


Java ConcurrentLinkedDeque getFirst()用法及代碼示例


java.util.concurrent.ConcurrentLinkedDeque.getFirst()方法是Java中的內置方法,它返回雙端隊列容器的第一個元素。

用法:

Conn_Linked_Deque.getFirst()

參數:該方法不接受任何參數。


返回值:該方法返回雙端隊列中存在的第一個元素。

異常:當雙端隊列為空時,該函數將引發NoSuchElementException。

以下示例程序旨在說明ConcurrentLinkedDeque.getFirst()方法:

程序1

/* Java Program to Demonstrate getFirst() 
   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 the Deque: " + cld); 
  
        // Displaying the first element 
        System.out.println("The first element is: " + 
                                      cld.getFirst()); 
    } 
}
輸出:
Elements in the Deque: [Welcome, To, Geeks, 4, Geeks]
The first element is: Welcome

示例2:

/* Java Program to Demonstrate getFirst() 
   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>(); 
  
        try { 
            // Displaying the first element 
            System.out.println("The first element "
                               + "is: " + cld.getFirst()); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
  
        // 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 the Deque: " + cld); 
  
        // Displaying the first element 
        System.out.println("The first element is: " + 
                                       cld.getFirst()); 
    } 
}
輸出:
java.util.NoSuchElementException
Elements in the Deque: [12, 43, 29, 16, 70]
The first element is: 12

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#getFirst()



相關用法


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