在contains()
的CopyOnWriteArrayList类的方法被用于获得指定的元素。如果该元素在列表中,则返回TRUE,否则返回FALSE。
语法如下
boolean contains(Object ob)
在这里,ob是要检查存在性的元素。要使用CopyOnWriteArrayList类,您需要导入以下包
import java.util.concurrent.CopyOnWriteArrayList;
以下是contains()
在Java中实现CopyOnWriteArrayList类方法的示例
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