Java中的Queue element() 方法
Queue接口的element()方法返回容器最前端的元素,但不从容器中删除元素。该方法返回队列的头。
该方法与peek()方法的不同之处在于,如果队列为空,它会抛出异常。
语法:
E element()
返回值: 该方法返回队列的头。
异常: 当队列为空并且调用该函数时,将抛出NoSuchElementException异常。
下面的程序演示了Queue的element()方法:
程序1: 使用LinkedList。
// Java程序演示Queue的元素()方法
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
//创建一个Queue对象
Queue<Integer> Q
= new LinkedList<Integer>();
//将数字添加到队列的末尾
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
//打印队列
System.out.println("Queue: " + Q);
//打印头
System.out.println("Queue's head: " + Q.element());
}
}
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
程序2: 使用ArrayDeque。
// Java程序演示Queue的元素()方法
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
//创建一个Queue对象
Queue<Integer> Q
= new ArrayDeque<Integer>();
//将数字添加到队列的末尾
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
//打印队列
System.out.println("Queue: " + Q);
//打印头
System.out.println("Queue's head: " + Q.element());
}
}
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
程序3: 使用LinkedBlockingDeque。
// Java程序演示Queue的元素()方法
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
//创建一个Queue对象
Queue<Integer> Q
= new LinkedBlockingDeque<Integer>();
//将数字添加到队列的末尾
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
//打印队列
System.out.println("Queue: " + Q);
//打印头
System.out.println("Queue's head: " + Q.element());
}
}
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
程序4: 使用ConcurrentLinkedDeque。
// Java程序演示Queue的element()方法
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.element());
}
}
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
以下程序说明该方法抛出的异常:
程序5: 说明 NoSuchElementException .
// Java程序演示Queue的element()方法
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// 创建Queue对象
Queue<Integer> Q
= new LinkedList<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.element());
Q.clear();
// 打印队列
System.out.println("Queue: " + Q);
try {
// Queue现在为空,因此会引发异常
System.out.println("Queue's head: " + Q.element());
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue: []
Exception: java.util.NoSuchElementException
参考来源: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#element–
极客教程