Java Deque getLast()方法

Java Deque getLast()方法

Deque接口getLast() 方法返回Deque的最后一个元素或尾部。它并不删除该元素。当Deque为空时,它会抛出一个异常。

语法

E getLast()

参数: 该方法不接受任何参数。

返回: 该方法返回最后一个元素或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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程