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


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


Deque接口的getLast()方法返回Deque的最後一個元素或尾部。它不會刪除元素。當雙端隊列為空時,它將引發異常。

用法:

E getLast()

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


返回值:Thie方法返回Deque的最後一個元素或尾部,但不刪除它。

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

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

示例1:借助LinkedList。

// Java Program Demonstrate getLast() 
// 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.getLast()); 
    } 
}
輸出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 74381793

示例2:借助ArrayDeque。

// Java Program Demonstrate getLast() 
// 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.getLast()); 
    } 
}
輸出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 74381793

示例3:借助ConcurrentLinkedDeque。

// Java Program Demonstrate getLast() 
// 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.getLast()); 
    } 
}
輸出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 74381793

示例4:借助LinkedBlockingDeque。

// Java Program Demonstrate getLast() 
// 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.getLast()); 
    } 
}
輸出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 74381793

示例5:

// Java Program Demonstrate getLast()() 
// 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.getLast()); 
  
        DQ.clear(); 
  
        // Deque is empty now hence exception 
        System.out.println("Deque's head: " + DQ.getLast()); 
    } 
}

輸出:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.getLast(LinkedList.java:257)
    at GFG.main(GFG.java:29)

參考: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#getLast–



相關用法


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