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


Java AbstractSequentialList clear()用法及代碼示例


Java中的AbstractSequentialList的clear()方法用於從列表中刪除所有元素。該調用返回後,該列表將為空。

用法:

public void clear()

參數:此函數沒有參數。


返回:該方法不返回任何值。它將刪除列表中的所有元素並將其清空。

以下示例說明了AbstractSequentialList.clear()方法:

範例1:

// Java code to demonstrate the working of 
// clear() method in AbstractSequentialList 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // creating an AbstractSequentialList 
        AbstractSequentialList<Integer> arr 
            = new LinkedList<Integer>(); 
  
        // using add() to initialize values 
        // [1, 2, 3, 4] 
        arr.add(1); 
        arr.add(2); 
        arr.add(3); 
        arr.add(4); 
  
        // list initially 
        System.out.println("The list initially:"
                           + arr); 
  
        // clear function used 
        arr.clear(); 
  
        // list after clearing all elements 
        System.out.println("The list after "
                           + "using clear() method:"
                           + arr); 
    } 
}
輸出:
The list initially:[1, 2, 3, 4]
The list after using clear() method:[]

範例2:

// Java code to demonstrate the working of 
// clear() method in AbstractSequentialList 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // creating an AbstractSequentialList 
        AbstractSequentialList<String> arr 
            = new LinkedList<String>(); 
  
        // using add() to initialize values 
        // [Geeks, For, ForGeeks, GeeksForGeeks] 
        arr.add("Geeks"); 
        arr.add("For"); 
        arr.add("ForGeeks"); 
        arr.add("GeeksForGeeks"); 
  
        // list initially 
        System.out.println("The list initially:"
                           + arr); 
  
        // clear function used 
        arr.clear(); 
  
        // list after clearing all elements 
        System.out.println("The list after "
                           + "using clear() method:"
                           + arr); 
    } 
}
輸出:
The list initially:[Geeks, For, ForGeeks, GeeksForGeeks]
The list after using clear() method:[]


相關用法


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