使用clear()AbstractList类的方法从列表中删除所有元素。使用该方法后,列表将没有任何元素。
语法如下
public void clear()
要使用AbstractList类,请导入以下包
import java.util.AbstractList;
以下是clear()在Java中实现AbstractlList类的方法的示例
import java.util.ArrayList;
import java.util.AbstractList;
public class Demo {
public static void main(String[] args) {
AbstractList<Integer>
myList = new ArrayList<Integer>();
myList.add(75);
myList.add(100);
myList.add(150);
myList.add(200);
myList.add(250);
myList.add(300);
myList.add(350);
myList.add(400);
System.out.println("Elements in the AbstractList = " + myList);
System.out.println("Count of Elements in the AbstractList = " + myList.size());
myList.clear();
System.out.println("\nElements in the updated AbstractList = " + myList);
System.out.println("Count of Elements in the updatedAbstractList = " + myList.size());
}
}输出结果
Elements in the AbstractList = [75, 100, 150, 200, 250, 300, 350, 400] Count of Elements in the AbstractList = 8 Elements in the updated AbstractList = [] Count of Elements in the updatedAbstractList = 0