Java中的 Deque element() 方法
Deque 接口的element()方法返回容器前面的元素。它不会删除容器中的元素。此方法返回 Deque 的头部。当 Deque 为空时,该方法会引发异常。
句法:
E element()
参数:此方法不接受任何参数。
返回:此方法返回容器前面的元素或 Deque 的头部。
异常:当 Deque 为空并调用函数时,函数抛出NoSuchElementException 。
下面的程序说明了 Deque 的 element() 方法:
程序 1:在LinkedList的帮助下。
Java
// Java Program Demonstrate element()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedList();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.element());
}
}
Java
// Java Program Demonstrate element()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new ArrayDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.element());
}
}
Java
// Java Program Demonstrate element()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new ConcurrentLinkedDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.element());
}
}
Java
// Java Program Demonstrate element()
// method of Deque
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedBlockingDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.element());
}
}
Java
// Java Program Demonstrate element()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedList();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.element());
DQ.clear();
// Deque is empty now hence exception
System.out.println("Deque's head: " + DQ.element());
}
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642
程序 2:在ArrayDeque的帮助下。
Java
// Java Program Demonstrate element()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new ArrayDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.element());
}
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642
程序 3:在ConcurrentLinkedDeque的帮助下。
Java
// Java Program Demonstrate element()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new ConcurrentLinkedDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.element());
}
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642
程序 4:在LinkedBlockingDeque的帮助下。
Java
// Java Program Demonstrate element()
// method of Deque
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedBlockingDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.element());
}
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642
方案二:
Java
// Java Program Demonstrate element()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedList();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.element());
DQ.clear();
// Deque is empty now hence exception
System.out.println("Deque's head: " + DQ.element());
}
}
输出:
Exception in thread "main" java.util.NoSuchElementException
at java.util.LinkedList.getFirst(LinkedList.java:244)
at java.util.LinkedList.element(LinkedList.java:663)
at GFG.main(GFG.java:29)
参考: https: Java/util/Deque.html#element–