clear java_Java ConcurrentLinkedDeque clear()用法及代码示例

本文详细介绍了Java中的ConcurrentLinkedDeque类的clear()方法,展示了如何使用该方法删除Deque中的所有元素。通过示例程序,读者可以清晰地看到在调用clear()前后Deque的状态变化,从而掌握该方法的使用方法。
摘要由CSDN通过智能技术生成

java.util.concurrent.ConcurrentLinkedDeque.clear()方法是Java中的内置方法,它删除了Deque中的元素。

用法:

public void clear()

参数:该方法不接受任何参数。

返回值:该函数不返回任何东西

以下示例程序旨在说明ConcurrentLinkedDeque.clear()方法:

程序1:

// Java Program to Demonstrate clear()

// method of ConcurrentLinkedDeque

import java.util.concurrent.*;

class GFG {

public static void main(String[] args)

{

// Creating an empty Deque

ConcurrentLinkedDeque cld

= new ConcurrentLinkedDeque();

// 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("ConcurrentLinkedDeque: "

+ cld);

// Remove all elements of the Deque using clear()

cld.clear();

// Displaying message

System.out.println("\nConcurrentLinkedDeque cleared\n");

// Displaying the Deque

System.out.println("ConcurrentLinkedDeque: "

+ cld);

}

}

输出:

ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]

ConcurrentLinkedDeque cleared

ConcurrentLinkedDeque: []

示例2:

// Java Program to Demonstrate clear()

// method of ConcurrentLinkedDeque

import java.util.concurrent.*;

class GFG {

public static void main(String[] args)

{

// Creating an empty Deque

ConcurrentLinkedDeque cld

= new ConcurrentLinkedDeque();

// Add elements into the Deque

cld.add(10);

cld.add(20);

cld.add(30);

cld.add(40);

cld.add(50);

// Displaying the Deque

System.out.println("ConcurrentLinkedDeque: "

+ cld);

// Remove all elements of the Deque using clear()

cld.clear();

// Displaying message

System.out.println("\nConcurrentLinkedDeque cleared\n");

// Displaying the Deque

System.out.println("ConcurrentLinkedDeque: "

+ cld);

}

}

输出:

ConcurrentLinkedDeque: [10, 20, 30, 40, 50]

ConcurrentLinkedDeque cleared

ConcurrentLinkedDeque: []

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值