若要检查列表是否为空,请使用isEmpty()
方法。如果列表为空,则返回TRUE,否则返回FALSE。
语法如下
boolean isEmpty()
要使用CopyOnWriteArrayList类,您需要导入以下包
import java.util.concurrent.CopyOnWriteArrayList;
以下是isEmpty()
在Java中实现CopyOnWriteArrayList类方法的示例
import java.util.concurrent.CopyOnWriteArrayList; public class Demo { public static void main(String[] args) { CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>(); arrList.add(30); arrList.add(40); arrList.add(60); arrList.add(70); arrList.add(90); arrList.add(100); arrList.add(120); System.out.println("CopyOnWriteArrayList = " + arrList); System.out.println("Is the CopyOnWriteArrayList empty? " + arrList.isEmpty()); } }
输出结果
CopyOnWriteArrayList = [30, 40, 60, 70, 90, 100, 120] Is the CopyOnWriteArrayList empty? False
让我们来看另一个例子
import java.util.concurrent.CopyOnWriteArrayList; public class Demo { public static void main(String[] args) { CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>(); arrList.add(30); arrList.add(40); arrList.add(60); arrList.add(70); arrList.add(90); arrList.add(100); arrList.add(120); System.out.println("CopyOnWriteArrayList = " + arrList); System.out.println("Is the CopyOnWriteArrayList empty? " + arrList.isEmpty()); arrList.clear(); System.out.println("Updated CopyOnWriteArrayList = " + arrList); System.out.println("Is the updated CopyOnWriteArrayList empty? " + arrList.isEmpty()); } }
输出结果
CopyOnWriteArrayList = [30, 40, 60, 70, 90, 100, 120] Is the CopyOnWriteArrayList empty? false Updated CopyOnWriteArrayList = [] Is the updated CopyOnWriteArrayList empty? True