Java 中 CopyOnWriteArrayList 的 contains() 方法
CopyOnWriteArrayList 类的 contains() 方法用于获取指定元素。如果元素在列表中,则返回 TRUE,否则返回 FALSE。
语法如下
boolean contains(Object ob)
此处,ob 是要检查是否存在性的元素。要操作 CopyOnWriteArrayList 类,你需要导入以下包
import java.util.concurrent.CopyOnWriteArrayList;
以下是使用 Java 中的 CopyOnWriteArrayList 类 contains() 方法的示例
示例
import java.util.concurrent.CopyOnWriteArrayList; public class Demo { public static void main(String[] args) { CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>(); arrList.add(100); arrList.add(250); arrList.add(400); arrList.add(500); arrList.add(650); arrList.add(700); arrList.add(800); System.out.println("CopyOnWriteArrayList Elements = " + arrList); System.out.println("The element at 2nd position = " + arrList.get(1)); System.out.println("Does the element exist in the List? = " + arrList.contains(400)); } }
输出
CopyOnWriteArrayList Elements = [100, 250, 400, 500, 650, 700, 800] The element at 2nd position = 250 Does the element exist in the List? = true
广告