ArrayList类clear()方法 (ArrayList Class clear() method)
clear() method is available in java.util package.
clear()方法在java.util包中可用。
clear() method is used to delete or clear all the elements from this Arraylist.
clear()方法用于删除或清除此Arraylist中的所有元素。
clear() method is a non-static method so it is accessible with the class object and if we try to access the method with the class name then we will get an error.
clear()方法是一种非静态方法,因此可以通过类对象进行访问,如果尝试使用类名称访问该方法,则会收到错误消息。
clear() method does not throw an exception at the time of clearing Arraylist.
clear()方法在清除Arraylist时不会引发异常。
Syntax:
句法:
public void clear();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is void, it returns nothing.
该方法的返回类型为void ,不返回任何内容。
Example:
例:
// Java program to demonstrate the example
// of void clear() method of ArrayList.
import java.util.*;
public class ClearOfArrayList {
public static void main(String[] args) {
// Create an ArrayList with initial
// capacity of storing elements
ArrayList < String > arr_l = new ArrayList < String > (10);
// By using add() method is to add
// elements in this ArrayList
arr_l.add("C");
arr_l.add("C++");
arr_l.add("JAVA");
arr_l.add("DOTNET");
arr_l.add("PHP");
arr_l.add("JAVA");
// Display ArrayList
System.out.println("ArrayList Elements :" + arr_l);
// By using clear() method is to
// remove all the elements from the ArrayList
arr_l.clear();
// Display ArrayList
System.out.println("arr_l.clear() : " + arr_l);
}
}
Output
输出量
ArrayList Elements :[C, C++, JAVA, DOTNET, PHP, JAVA]
arr_l.clear() : []
翻译自: https://www.includehelp.com/java/arraylist-clear-method-with-example.aspx