📌  相关文章
📜  Java中的 ConcurrentLinkedDeque removeLast() 方法

📅  最后修改于: 2022-05-13 01:55:43.694000             🧑  作者: Mango

Java中的 ConcurrentLinkedDeque removeLast() 方法

ConcurrentLinkedDeque.removeLast()是Java中的一个内置函数,用于删除双端队列中的最后一个元素。如果双端队列为空,该函数将引发NoSuchElementException

句法:

Conn_Linked_Deque.removeLast()

参数:该函数不接受任何参数。

返回值:该函数返回双端队列中的最后一个元素。

异常:如果双端队列为空,该函数将引发NoSuchElementException

下面的程序说明了 removeLast() 方法:

程序 1:该程序涉及带有Integer元素的双端队列。

// Java Program Demonstrate removeLast() 
// method of ConcurrentLinkedDeque   
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque cld = 
                      new ConcurrentLinkedDeque();
  
        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 last element
        System.out.println("Element removed : "
                           + cld.peekLast());
  
        // Remove the Last element
        cld.removeLast();
  
        // Displaying the elements
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld);
    }
}
输出:
Elements inthe LinkedDeque: [475, 1009, 70, 12]
Element removed : 12
Elements inthe LinkedDeque: [475, 1009, 70]

程序 2:该程序涉及带有String元素的双端队列。

// Java Program Demonstrate removeLast() 
// method of ConcurrentLinkedDeque   
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque cld = 
                           new ConcurrentLinkedDeque();
  
        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 last element
        System.out.println("Element removed : "
                           + cld.peekLast());
  
        // Remove the Last element
        cld.removeLast();
  
        // Displaying the elements
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld);
    }
}
输出:
Elements inthe LinkedDeque: [Geeks, GeeksforGeeks, Gfg, GFG]
Element removed : GFG
Elements inthe LinkedDeque: [Geeks, GeeksforGeeks, Gfg]

参考:https: Java/util/concurrent/ConcurrentLinkedDeque.html#removeLast()