Java 语言中 CopyOnWriteArrayList 中的 isEmpty() 方法
若要检查列表是否为空,可以使用 isEmpty() 方法。如果列表为空,则返回 TRUE,否则返回 FALSE。
语法如下
boolean isEmpty()
要使用 CopyOnWriteArrayList 类,你需要导入以下包
import java.util.concurrent.CopyOnWriteArrayList;
下面是一个在 Java 中实现 CopyOnWriteArrayList 类 isEmpty() 方法的示例
示例
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
广告