Java中的ConcurrentLinkedQue add()方法

这个 JAVAutil。同时发生的ConcurrentLinkedEque。添加() 是Java中的一个内置函数,它在deque的末尾插入指定的元素。

null

语法:

conn_linked_deque.add(elem)

参数: 该方法只接受一个参数 埃伦 将被添加到ConcurentLinkedQue的尾部。

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

例外情况: 该方法将抛出 空指针异常 当传递给函数的参数为 无效的 .由于其有限性,此方法永远不会抛出 非法国家例外 或者返回false。

下面的程序演示了ConcurrentLinkedQue。add()方法:

项目1: 此程序涉及整数类型的ConcurrentLinkedQue。

// Java Program Demonstrate add()
// method of ConcurrentLinkedDeque
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
public static void main(String[] args)
{
ConcurrentLinkedDeque<Integer> cld =
new ConcurrentLinkedDeque<Integer>();
cld.add( 12 );
cld.add( 110 );
cld.add( 55 );
cld.add( 76 );
// Displaying the existing LinkedDeque
System.out.println( "Initial Elements in"
+ "the LinkedDeque: " + cld);
// Insert a new element in the  LinkedDeque
cld.add( 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: 此程序涉及字符串类型的ConcurrentLinkedQue,在 无效的 作为参数传递给函数。

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


输出:

Initial Elements inthe LinkedDeque: [Gfg, GFG, Geeksforgeeks, Contribute]
NullPointerExceptionthrown
Initial Elements inthe LinkedDeque: [Gfg, GFG, Geeksforgeeks, Contribute, Geek Classes]

参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#add()

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享