当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。