Java的Deque getFirst() 方法
Deque 接口的getFirst()方法返回Deque的第一个元素或头部。它不会删除元素。当 Deque 为空时抛出异常。
句法:
E getFirst()
参数:此方法不接受任何参数。
返回: Thie 方法返回 Deque 的第一个元素或头部,但不删除它。
例外:函数抛出NoSuchElementException异常时,双端队列为空并且函数被调用。
下面的程序说明了 Deque 的 getFirst() 方法:
方案一:借助LinkedList 。
// Java Program Demonstrate getFirst()
// 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.getFirst());
}
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642
方案二:借助ArrayDeque 。
// Java Program Demonstrate getFirst()
// 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.getFirst());
}
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642
程序 3:在LinkedBlockingDeque的帮助下。
// Java Program Demonstrate getFirst()
// 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.getFirst());
}
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642
程序 4:借助ConcurrentLinkedDeque 。
// Java Program Demonstrate getFirst()
// 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.getFirst());
}
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642
方案二:
// Java Program Demonstrate getFirst()
// method of Deque when it is empty
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.getFirst());
DQ.clear();
// Deque is empty now hence exception
System.out.println("Deque's head: " + DQ.getFirst());
}
}
输出:
Exception in thread "main" java.util.NoSuchElementException
at java.util.LinkedList.getFirst(LinkedList.java:244)
at GFG.main(GFG.java:29)
参考:https://docs.oracle.com/javase/8/docs/api/的Java/util/Deque.html#getFirst-