📜  Java中的双端队列 getLast() 方法

📅  最后修改于: 2022-05-13 01:55:05.456000             🧑  作者: Mango

Java中的双端队列 getLast() 方法

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

句法:

E getLast()

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

返回:此方法返回 Deque 的最后一个元素或尾部,但不删除它。

异常:当 Deque 为空并调用函数时,函数抛出NoSuchElementException

下面的程序说明了 Deque 的 getLast() 方法:

程序 1:LinkedList的帮助下。

Java
// 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 DQ
            = new LinkedList();
  
        // 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());
    }
}


Java
// 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 DQ
            = new ArrayDeque();
  
        // 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());
    }
}


Java
// 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 DQ
            = new ConcurrentLinkedDeque();
  
        // 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());
    }
}


Java
// 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 DQ
            = new LinkedBlockingDeque();
  
        // 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());
    }
}


Java
// 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 DQ
            = new LinkedList();
  
        // 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());
    }
}


输出:
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 74381793

程序 2:ArrayDeque的帮助下。

Java

// 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 DQ
            = new ArrayDeque();
  
        // 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

// 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 DQ
            = new ConcurrentLinkedDeque();
  
        // 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

// 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 DQ
            = new LinkedBlockingDeque();
  
        // 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

// 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 DQ
            = new LinkedList();
  
        // 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: Java/util/Deque.html#getLast–