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

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

Java中的 ConcurrentLinkedDeque clear() 方法

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: []

方案二:

// 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: []

参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/concurrent/ConcurrentLinkedDeque.html#clear–