Java ArrayList clear() 方法



描述

Java ArrayList clear() 方法会移除此列表中的所有元素。此调用返回后,列表将为空。

声明

以下是 java.util.ArrayList.clear() 方法的声明

public void clear()

参数

返回值

此方法不返回值。

异常

清空整数型 ArrayList 示例

以下示例演示了 Java ArrayList clear() 方法的用法。在此示例中,我们使用的是整数。第一步,我们填充 arrayList 对象并打印它。然后我们打印 arrayList 对象的大小并执行清空操作。清空后,我们打印 arrayList 对象的大小,现在为 0。

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
     
      // create an empty array list
      ArrayList<Integer> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(20);
      arrayList.add(30);
      arrayList.add(10);

      // let us print all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);    

      // finding size of this arrayList
      int retval = arrayList.size();
      System.out.println("ArrayList consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      arrayList.clear();
      retval = arrayList.size();
      System.out.println("Now, ArrayList consists of "+ retval +" elements");
   }
}   

输出

让我们编译并运行上述程序,这将产生以下结果:

ArrayList = [20, 30, 10]
ArrayList consists of 3 elements
Performing clear operation !!
Now, ArrayList consists of 0 elements

清空字符串型 ArrayList 示例

以下示例演示了 Java ArrayList clear() 方法的用法。在此示例中,我们使用的是字符串。第一步,我们填充 arrayList 对象并打印它。然后我们打印 arrayList 对象的大小并执行清空操作。清空后,我们打印 arrayList 对象的大小,现在为 0。

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
     
      // create an empty array list
      ArrayList<String> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");

      // let us print all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);    

      // finding size of this arrayList
      int retval = arrayList.size();
      System.out.println("ArrayList consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      arrayList.clear();
      retval = arrayList.size();
      System.out.println("Now, ArrayList consists of "+ retval +" elements");
   }
}   

输出

让我们编译并运行上述程序,这将产生以下结果:

ArrayList = [A, B, C]
ArrayList consists of 3 elements
Performing clear operation !!
Now, ArrayList consists of 0 elements

清空对象型 ArrayList 示例

以下示例演示了 Java ArrayList clear() 方法的用法。在此示例中,我们使用的是 Student 对象。第一步,我们填充 arrayList 对象并打印它。然后我们打印 arrayList 对象的大小并执行清空操作。清空后,我们打印 arrayList 对象的大小,现在为 0。

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
     
      // create an empty arrayList
      ArrayList<Student> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(new Student(1, "Julie"));
      arrayList.add(new Student(2, "Robert"));
      arrayList.add(new Student(3, "Adam"));

      // let us print all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);    

      // finding size of this arrayList
      int retval = arrayList.size();
      System.out.println("arrayList consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      arrayList.clear();
      retval = arrayList.size();
      System.out.println("Now, arrayList consists of "+ retval +" elements");
   } 		
} 

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}  

输出

让我们编译并运行上述程序,这将产生以下结果:

ArrayList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
arrayList consists of 3 elements
Performing clear operation !!
Now, arrayList consists of 0 elements
java_util_arraylist.htm
广告