📌  相关文章
📜  Java中的 LinkedBlockingDeque remove() 方法

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

Java中的 LinkedBlockingDeque remove() 方法

LinkedBlockingDequeremove()方法移除了 Deque 容器的头部。如果 Deque 容器为空,该方法将引发NoSuchElementException

句法:

public E remove()

参数:此方法不接受任何参数。

返回:此方法不返回任何内容。

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

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

方案一:

// Java Program Demonstrate remove()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(7855642);
        LBD.add(35658786);
        LBD.add(5278367);
        LBD.add(74381793);
  
        // print Dequeue
        System.out.println("Linked Blocking Deque: " + LBD);
  
        // removes the front element
        LBD.remove();
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}
输出:
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Linked Blocking Deque: [35658786, 5278367, 74381793]

方案二:

// Java Program Demonstrate remove()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws NoSuchElementException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque(6);
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(7855642);
        LBD.add(35658786);
        LBD.add(5278367);
        LBD.add(74381793);
  
        // print Dequeue
        System.out.println("Linked Blocking Deque: " + LBD);
  
        // Deque is empty
        LBD.clear();
  
        // throws an exception
        LBD.remove();
    }
}

输出:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.concurrent.LinkedBlockingDeque.removeFirst(LinkedBlockingDeque.java:453)
    at java.util.concurrent.LinkedBlockingDeque.remove(LinkedBlockingDeque.java:672)
    at GFG.main(GFG.java:30)

参考: https: Java/util/concurrent/LinkedBlockingDeque.html#remove–