Java 中的 AbstractList 类的 clear() 方法
使用 AbstractList 类的 clear() 方法从列表中移除所有元素。使用该方法后,列表中将不包含任何元素。
语法如下
public void clear()
若要使用 AbstractList 类,请导入以下包
import java.util.AbstractList;
以下是一个在 Java 中实现 AbstractlList 类 clear() 方法的示例
示例
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("
Elements 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
广告