Java中的队列remove()方法
Queue 接口的remove()方法返回并移除容器前面的元素。它删除了容器的头部。当 Queue 为空时,该方法会引发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 Q
= new LinkedList();
// 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 Q
= new ArrayDeque();
// 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 Q
= new LinkedBlockingDeque();
// 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 Program Demonstrate remove()
// method of Queue
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue Q
= new ConcurrentLinkedDeque();
// 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
下面的程序说明了此方法引发的异常:
程序 5:显示NoSuchElementException 。
// 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 Q
= new LinkedList();
// Add numbers to end of Queue
Q.add(423);
Q.add(3432);
// 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());
// print queue
System.out.println("Queue: " + Q);
try {
// Queue is empty now hence exception
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: Java/util/Queue.html#remove–