隊列接口的remove()方法返回並移除容器前麵的元素。刪除容器的頭部。隊列為空時,此方法將引發NoSuchElementException。
用法:
E remove()
返回值:此方法返回隊列的頭部。
異常:隊列為空時,該函數將引發NoSuchElementException。
以下示例程序旨在說明隊列的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 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<Integer> Q
= new ConcurrentLinkedDeque<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
以下示例程序旨在說明此方法引發的異常:
示例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<Integer> Q
= new LinkedList<Integer>();
// 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://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#remove–
相關用法
- Java Queue add()用法及代碼示例
- Java Queue offer()用法及代碼示例
- Java Queue element()用法及代碼示例
- Java Queue peek()用法及代碼示例
- Java Queue poll()用法及代碼示例
- Java LinkedBlockingDeque remove()用法及代碼示例
- Java HashMap remove()用法及代碼示例
- Java Stack remove(int)用法及代碼示例
- Java ArrayDeque remove()用法及代碼示例
- Java LinkedList remove()用法及代碼示例
- Java ConcurrentSkipListSet remove()用法及代碼示例
- Java LinkedBlockingQueue remove()用法及代碼示例
- Java EnumMap remove()用法及代碼示例
- Java PriorityBlockingQueue remove()用法及代碼示例
- Java Map remove()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Queue remove() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。