爪哇。util。同时发生的ConcurrentLinkedEque。offerLast()方法是Java中的一个内置方法,它将指定的元素作为参数插入到deque的末尾。
null
语法:
Conn_Linked_Deque.offerLast(Object elem)
参数: 该方法接受一个参数 埃伦 哪种元素要插入到deque的末尾。
返回值: 函数返回 符合事实的 如果元素成功添加到deque中并返回 错误的 否则
例外情况: 函数抛出一个 空指针异常 如果传递的参数为 无效的 .
下面的程序说明了 ConcurrentLinkedEque。offerLast() 方法:
方案1 :
/* Java Program to Demonstrate offerLast() 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()); /* Insert an element at the tail of the deque */ if (cld.offerLast( "GFG" )) { // Displaying the Last element System.out.println( "The Inserted element is: " + cld.getLast()); } // 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 The Inserted element is: GFG Elements in Deque: [Welcome, To, Geeks, 4, Geeks, GFG] The Last element is: GFG
方案2 :
/* Java Program to Demonstrate offerLast() 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>(); // 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()); try { cld.offerLast( null ); } catch (Exception e) { System.out.println(e); } /* Insert an element at the tail of the deque */ if (cld.offerLast( 24 )) { // Displaying the Last element System.out.println( "The Inserted element is: " + cld.getLast()); } // 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: [12, 43, 29, 16, 70] The Last element is: 70 java.lang.NullPointerException The Inserted element is: 24 Elements in Deque: [12, 43, 29, 16, 70, 24] The Last element is: 24
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END