当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。