这个 JAVAutil。同时发生的ConcurrentLinkedEque。getLast() 方法是Java中的一个内置方法,它返回deque容器的最后一个元素。
null
语法:
Conn_Linked_Deque.getLast()
参数: 该方法不接受任何参数。
返回值: 该方法返回Deque中的最后一个元素。
例外情况: 函数抛出一个 非接触性异常 当deque是空的。
下面的程序说明了 ConcurrentLinkedEque。getLast() 方法:
方案1 :
/* Java Program to Demonstrate getLast() method of ConcurrentLinkedDeque */ import java.util.concurrent.*; class GFG { public static void main(String[] args) { // Creating an empty Deque ConcurrentLinkedDeque<String> cld = new ConcurrentLinkedDeque<String>(); // Add elements into the Deque cld.add( "Welcome" ); cld.add( "To" ); cld.add( "Geeks" ); cld.add( "4" ); cld.add( "Geeks" ); // Displaying the Deque System.out.println( "Elements in Deque: " + cld); // Displaying the Last element System.out.println( "The Last element is: " + cld.getLast()); } } |
输出:
Elements in Deque: [Welcome, To, Geeks, 4, Geeks] The Last element is: Geeks
项目2:
/* Java Program to Demonstrate getLast() method of ConcurrentLinkedDeque */ import java.util.concurrent.*; class GFG { public static void main(String[] args) { // Creating an empty Deque ConcurrentLinkedDeque<Integer> cld = new ConcurrentLinkedDeque<Integer>(); try { // Displaying the Last element System.out.println( "The Last element " + "is: " + cld.getLast()); } catch (Exception e) { // Displaying the Exception System.out.println(e); } // Add elements into the Deque cld.add( 12 ); cld.add( 43 ); cld.add( 29 ); cld.add( 16 ); cld.add( 70 ); // Displaying the Deque System.out.println( "Elements in " + "Deque: " + cld); // Displaying the Last element System.out.println( "The Last element is: " + cld.getLast()); } } |
输出:
java.util.NoSuchElementException Elements in Deque: [12, 43, 29, 16, 70] The Last element is: 70
参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#getLast()
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END