Java 中的 ConcurrentLinkedDeque addLast()方法

原文:https://www . geeksforgeeks . org/concurrentlinkedeque-addlast-method-in-Java/

java . util . concurrentlinkedequest . addlast()是 Java 中的一个内置函数,它将指定的元素插入到 deque 的末尾。

语法:

conn_linked_deque.addLast*(elem)*

参数:该方法只接受单个参数 elem ,该参数将被添加到 concurrentlinkedrequest 的末尾。

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

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

下面的程序说明了Java . util . concurrentlinkedequest . addlast()方法的使用:

程序 1: 该程序涉及一个整数类型的并发链接请求。

// Java Program Demonstrate addLast()
// method of ConcurrentLinkedDeque 

import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque<Integer> cld = 
                      new ConcurrentLinkedDeque<Integer>();
        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:空值作为参数传递给函数时,该程序涉及带有异常处理的整数类型的并发链接请求。

// Java Program Demonstrate addLast()
// method of ConcurrentLinkedDeque 

import java.util.concurrent.*;

class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque<String> cld = 
                        new ConcurrentLinkedDeque<String>();

        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://docs . Oracle . com/javase/7/docs/API/Java/util/concurrentlinkedeque . html # addLast()