Java中的队列remove()方法
Queue接口 的 remove() 方法会返回并删除容器前面的元素,也就是删除容器头。当队列为空时,该方法会抛出 NoSuchElementException 异常。
语法:
E remove()
返回值: 该方法返回队列 头部 上的元素。
异常: 当队列为空时,该方法抛出 NoSuchElementException 异常。
下面的程序演示Queue中的remove()方法:
程序1: 使用 LinkedList 。
// Java Program Demonstrate remove()
// method of Queue
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedList<Integer>();
// Add numbers to end of Queue
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// print queue
System.out.println("Queue: " + Q);
// print head and deletes the head
System.out.println("Queue's head: " + Q.remove());
// print head and deleted the head
System.out.println("Queue's head: " + Q.remove());
}
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786
程序2: 使用 ArrayDeque 。
// Java Program Demonstrate remove()
// method of Queue
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new ArrayDeque<Integer>();
// Add numbers to end of Queue
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// print queue
System.out.println("Queue: " + Q);
// print head and deletes the head
System.out.println("Queue's head: " + Q.remove());
// print head and deleted the head
System.out.println("Queue's head: " + Q.remove());
}
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786
程序3: 使用 LinkedBlockingDeque 。
// Java Program Demonstrate remove()
// method of Queue
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of Queue
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// print queue
System.out.println("Queue: " + Q);
// print head and deletes the head
System.out.println("Queue's head: " + Q.remove());
// print head and deleted the head
System.out.println("Queue's head: " + Q.remove());
}
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786
程序4: 使用 ConcurrentLinkedDeque 帮助实现。
// Java程序演示remove()
// Queue的方法
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// 创建Queue对象
Queue<Integer> Q
= new ConcurrentLinkedDeque<Integer>();
// 将数字添加到Queue末尾
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// 打印队列
System.out.println("Queue: " + Q);
// 打印头并删除头部
System.out.println("Queue's head: " + Q.remove());
// 打印头并删除头部
System.out.println("Queue's head: " + Q.remove());
}
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786
下面程序说明 此方法抛出的异常 :
程序5: 演示 NoSuchElementException .
// Java程序演示remove()
// Queue的方法
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// 创建Queue对象
Queue<Integer> Q
= new LinkedList<Integer>();
// 将数字添加到Queue末尾
Q.add(423);
Q.add(3432);
// 打印队列
System.out.println("Queue: " + Q);
// 打印头并删除头部
System.out.println("Queue's head: " + Q.remove());
// 打印头并删除头部
System.out.println("Queue's head: " + Q.remove());
// 打印队列
System.out.println("Queue: " + Q);
try {
// Queue现在为空,因此会引发异常
System.out.println("Queue's head: " + Q.element());
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
Queue: [423, 3432]
Queue's head: 423
Queue's head: 3432
Queue: []
Exception: java.util.NoSuchElementException
参考: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#remove–
极客教程