當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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<String> cld 
            = new ConcurrentLinkedDeque<String>(); 
  
        // 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<Integer> cld 
            = new ConcurrentLinkedDeque<Integer>(); 
  
        // 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–



相關用法


注:本文由純淨天空篩選整理自Aishwarya Jayashankar大神的英文原創作品 ConcurrentLinkedDeque clear() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。