當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Deque getFirst()用法及代碼示例


雙端隊列接口的getFirst()方法返回雙端隊列的第一個元素或頭。它不會刪除元素。當雙端隊列為空時,它將引發異常。

用法:

E getFirst()

參數:此方法不接受任何參數。


返回值:Thie方法返回第一個元素或雙端隊列的頭部,但不刪除它。

異常:當Deque為空並調用該函數時,該函數將引發NoSuchElementException。

以下示例程序旨在說明Deque的getFirst()方法:

示例1:借助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<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.getFirst()); 
    } 
}
輸出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

示例2:借助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<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.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<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.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<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.getFirst()); 
    } 
}
輸出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

示例2:

// 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<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.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–



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Deque getFirst() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。