方法clear()在Java中做什么?
类java.util.ArrayList的clear()方法从此列表中删除所有元素。该调用返回后,该列表将为空。
示例
import java.util.ArrayList;public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<Integer> arrlist = new ArrayList<Integer>(5);
arrlist.add(20);
arrlist.add(30);
arrlist.add(10);
arrlist.add(50);
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
int retval = arrlist.size();
System.out.println("List consists of "+ retval +" elements");
System.out.println("Performing clear operation !!");
arrlist.clear();
retval = arrlist.size();
System.out.println("Now, list consists of "+ retval +" elements");
}
}
输出结果
Number = 20Number = 30
Number = 10
Number = 50
List consists of 4 elements
Performing clear operation !!
Now, list consists of 0 elements
以上是 方法clear()在Java中做什么? 的全部内容, 来源链接: utcz.com/z/338249.html