📌  相关文章
📜  Java中的 ConcurrentLinkedDeque addLast() 方法

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

Java中的 ConcurrentLinkedDeque addLast() 方法

Java .util.concurrent.ConcurrentLinkedDeque.addLast()是Java中的一个内置函数,它将指定的元素插入到双端队列的末尾。

句法:

conn_linked_deque.addLast(elem)

参数:该方法仅接受要添加到 ConcurrentLinkedDeque 末尾的单个参数elem

返回值:该函数没有返回值。

异常:当传递给函数的参数为 null时,该方法将抛出NullPointerException 。由于其有界性质,此方法永远不会抛出IllegalStateException或返回 false。

下面的程序说明了Java.util.concurrent.ConcurrentLinkedDeque.addLast()方法的使用:

方案 1:该方案涉及 Integer 类型的 ConcurrentLinkedDeque。

// Java Program Demonstrate addLast()
// method of ConcurrentLinkedDeque 
  
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque cld = 
                      new ConcurrentLinkedDeque();
        cld.addLast(12);
        cld.addLast(110);
        cld.addLast(55);
        cld.addLast(76);
  
        // Displaying the existing LinkedDeque
        System.out.println("Initial Elements in"
                           + "the LinkedDeque: " + cld);
  
        // Insert a new element in the  LinkedDeque
        cld.addLast(21);
  
        // Displaying the modified LinkedDeque
        System.out.println("Initial Elements in"
                           + "the LinkedDeque: " + cld);
    }
}
输出:
Initial Elements inthe LinkedDeque: [12, 110, 55, 76]
Initial Elements inthe LinkedDeque: [12, 110, 55, 76, 21]

程序 2:该程序涉及一个 Integer 类型的 ConcurrentLinkedDeque,当null作为参数传递给函数时具有异常处理。

// Java Program Demonstrate addLast()
// method of ConcurrentLinkedDeque 
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque cld = 
                        new ConcurrentLinkedDeque();
  
        cld.addLast("Geeks");
        cld.addLast("Geek");
        cld.addLast("Gfg");
        cld.addLast("Contribute");
  
        // Displaying the existing LinkedDeque
        System.out.println("Initial Elements in"
                           + "the LinkedDeque: " + cld);
  
        /* Exception thrown when null 
             is passed as parameter*/
        try {
            cld.addLast(null);
        }
        catch (NullPointerException e) {
            System.out.println("NullPointerException"
                               + "thrown");
        }
  
        // Insert a new element in the  LinkedDeque
        cld.addLast("Sudo Placement");
  
        // Displaying the modified LinkedDeque
        System.out.println("Initial Elements in"
                           + "the LinkedDeque: " + cld);
    }
}
输出:
Initial Elements inthe LinkedDeque: [Geeks, Geek, Gfg, Contribute]
NullPointerExceptionthrown
Initial Elements inthe LinkedDeque: [Geeks, Geek, Gfg, Contribute, Sudo Placement]

参考: https: Java/util/concurrent/ConcurrentLinkedDeque.html#addLast()