Deque Interface的element()方法返回位於容器前麵的元素。它不會刪除容器中的元素。此方法返回雙端隊列的頭部。當雙端隊列為空時,該方法將引發異常。
用法:
E element()
參數:此方法不接受任何參數。
返回值:Thie方法將元素返回到容器的頂部或雙端隊列的頭部。
異常:當Deque為空並調用該函數時,該函數將引發NoSuchElementException。
以下示例程序旨在說明Deque的element()方法:
示例1:借助LinkedList。
// 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<Integer> DQ
= new LinkedList<Integer>();
// 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
示例2:借助ArrayDeque。
// 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<Integer> DQ
= new ArrayDeque<Integer>();
// 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 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<Integer> DQ
= new ConcurrentLinkedDeque<Integer>();
// 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 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<Integer> DQ
= new LinkedBlockingDeque<Integer>();
// 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
示例2:
// 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<Integer> DQ
= new LinkedList<Integer>();
// 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://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#element–
相關用法
- Java Deque contains()用法及代碼示例
- Java Deque add()用法及代碼示例
- Java Deque offer()用法及代碼示例
- Java Deque iterator()用法及代碼示例
- Java Deque getLast()用法及代碼示例
- Java Deque getFirst()用法及代碼示例
- Java Deque offerFirst()用法及代碼示例
- Java Deque descendingIterator()用法及代碼示例
- Java Deque offerLast()用法及代碼示例
- Java Deque addLast()用法及代碼示例
- Java Deque addFirst()用法及代碼示例
- Java LinkedBlockingDeque element()用法及代碼示例
- Java Queue element()用法及代碼示例
- Java ConcurrentLinkedDeque element()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Deque element() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。