Java ConcurrentLinkedDeque addLast()方法

Java ConcurrentLinkedDeque addLast()方法

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

语法

conn_linked_deque.addLast(elem)

参数: 该方法只接受一个参数 elem ,它将被添加到ConcurrentLinkedDeque的末端。

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

异常: 当传递给该函数的参数为空时,该方法将抛出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<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: 该程序涉及一个整数类型的并发连接的Deque,当null作为参数传递给函数时有异常处理。

// 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/concurrent/ConcurrentLinkedDeque.html#addLast()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程