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


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


ConcurrentLinkedDeque.removeFirst()是Java中的內置函數,可從雙端隊列容器中刪除第一個元素。如果此雙端隊列為空,則該函數將引發NoSuchElementException。

用法:

Conn_Linked_Deque.removeFirst()

參數:該函數不接受任何參數。


返回值:該函數返回雙端隊列中的第一個元素。

異常:如果此雙端隊列為空,則該函數將引發NoSuchElementException。

以下示例程序旨在說明removeFirst()方法:

示例1:此程序涉及帶有Integer元素的雙端隊列。

// Java Program to demonstrate removeFirst()  
// 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(70); 
        cld.addFirst(1009); 
        cld.addFirst(475); 
  
        // Displaying the existing LinkedDeque 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
  
        // Display the first element 
        System.out.println("Element removed : "
                           + cld.peekFirst()); 
  
        // Remove the first element 
        cld.removeFirst(); 
  
        // Displaying the elements 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
    } 
}
輸出:
Elements inthe LinkedDeque: [475, 1009, 70, 12]
Element removed : 475
Elements inthe LinkedDeque: [1009, 70, 12]

示例2:該程序涉及帶有String元素的雙端隊列。

// Java Program Demonstrate removeFirst()  
// method of ConcurrentLinkedDeque    
  
import java.util.concurrent.*; 
  
class ConcurrentLinkedDequeDemo { 
    public static void main(String[] args) 
    { 
        ConcurrentLinkedDeque<String> cld =  
                      new ConcurrentLinkedDeque<String>(); 
  
        cld.addFirst("GFG"); 
        cld.addFirst("Gfg"); 
        cld.addFirst("GeeksforGeeks"); 
        cld.addFirst("Geeks"); 
  
        // Displaying the existing LinkedDeque 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
  
        // Display the first element 
        System.out.println("Element removed : "
                           + cld.peekFirst()); 
  
        // Remove the first element 
        cld.removeFirst(); 
  
        // Displaying the elements 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
    } 
}
輸出:
Elements inthe LinkedDeque: [Geeks, GeeksforGeeks, Gfg, GFG]
Element removed : Geeks
Elements inthe LinkedDeque: [GeeksforGeeks, Gfg, GFG]

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



相關用法


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