📜  在 Java 中清空 ArrayList 的JavaJava

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

在 Java 中清空 ArrayList 的JavaJava

ArrayList 类是一个可调整大小的数组,存在于“Java.util 包”中。 Java中内置数组和 ArrayList 之间的区别在于,数组的大小不能修改(即,如果您想向/从数组中添加/添加或删除元素,则必须创建一个新数组。但是,可以在 ArrayList 中添加/追加或删除元素,而无需创建新数组。

方法:

  1. 使用clear()方法。
  2. 使用removeAll()方法。

方法一:使用clear()方法作为Java中ArrayList的clear()方法,用于删除ArrayList中的所有元素。此调用返回后,ArrayList 将完全为空。

句法:

public void clear() ;

参数: clear函数不带参数

返回值:此方法不返回任何值。

例外:不适用

例子:

Java
// Java Program  to empty an ArrayList in Java
  
// Illustrating clear function
import java.util.ArrayList;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] arg)
    {
  
        // Create an empty array list
        // with an initial capacity of 4
        ArrayList numbers
            = new ArrayList(4);
  
        // Use add() method to add elements
        // in numbers ArrayList
        numbers.add("10");
        numbers.add("20");
        numbers.add("30");
        numbers.add("40");
  
        // Printing numbers ArrayList
        System.out.println("Numbers ArrayList : "
                           + numbers);
  
        // Finding size of numbers ArrayList
        int numbers_size = numbers.size();
  
        // Display message
        System.out.println("Numbers ArrayList consists of: "
                           + numbers_size + " elements");
  
        // Display Message to between changes made in
        // ArrayList
  
        // System.out.println("Performing clear operation by
        // using clear function");
  
        // Using clear function
        numbers.clear();
  
        int numbers_size_new = numbers.size();
  
        // Printing new ArrayList
        System.out.println(
            "Finally Numbers ArrayList consists of: "
            + numbers_size_new + " elements");
    }
}


Java
// Java Program  to empty an ArrayList in Java
// Java code to illustrate removeAll function
  
// Importing ArrayList library
import java.util.ArrayList;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] arg)
    {
  
        // Create an empty array list
        // with an initial capacity of 4
        ArrayList numbers
            = new ArrayList(4);
  
        // Using add() method to add elements in numbers
        // ArrayList
        numbers.add("10");
        numbers.add("20");
        numbers.add("30");
        numbers.add("40");
  
        // Printing numbers ArrayList
        System.out.println("Numbers ArrayList : "
                           + numbers);
  
        // Finding size of numbers ArrayList
        int numbers_size = numbers.size();
  
        // Display message
        System.out.println("Numbers ArrayList consists of "
                           + numbers_size + " elements");
  
        // Display Messege
        System.out.println(
            "Performing clear operation by using clear function");
  
        // Using removeAll function
        numbers.removeAll(numbers);
  
        // Displaying final ArrayList count of elements
        int numbers_size_new = numbers.size();
        System.out.println(
            "Finally Numbers ArrayList consists of "
            + numbers_size_new + " elements");
    }
}


输出
Numbers ArrayList : [10, 20, 30, 40]
Numbers ArrayList consists of 4 elements
Performing clear operation by using clear function
Finally Numbers ArrayList consists of 0 elements

方法2:使用removeAll()方法作为ArrayList类的这个方法,用于从这个列表中删除指定集合中包含的所有元素。

句法:

public boolean removeAll(Collection c) ;

参数:此方法将集合 c 作为包含要从此列表中删除的元素的参数。

返回值:如果此列表因调用而更改,则此方法返回 true。

异常/秒:此方法抛出NullPointerException 如果此列表包含空元素并且指定的集合不允许空元素(可选),或者指定的集合为空。

例子:

Java

// Java Program  to empty an ArrayList in Java
// Java code to illustrate removeAll function
  
// Importing ArrayList library
import java.util.ArrayList;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] arg)
    {
  
        // Create an empty array list
        // with an initial capacity of 4
        ArrayList numbers
            = new ArrayList(4);
  
        // Using add() method to add elements in numbers
        // ArrayList
        numbers.add("10");
        numbers.add("20");
        numbers.add("30");
        numbers.add("40");
  
        // Printing numbers ArrayList
        System.out.println("Numbers ArrayList : "
                           + numbers);
  
        // Finding size of numbers ArrayList
        int numbers_size = numbers.size();
  
        // Display message
        System.out.println("Numbers ArrayList consists of "
                           + numbers_size + " elements");
  
        // Display Messege
        System.out.println(
            "Performing clear operation by using clear function");
  
        // Using removeAll function
        numbers.removeAll(numbers);
  
        // Displaying final ArrayList count of elements
        int numbers_size_new = numbers.size();
        System.out.println(
            "Finally Numbers ArrayList consists of "
            + numbers_size_new + " elements");
    }
}
输出
Numbers ArrayList : [10, 20, 30, 40]
Numbers ArrayList consists of 4 elements
Performing clear operation by using clear function
Finally Numbers ArrayList consists of 0 elements