java 中 contains(obj o) 方法做什么?
java.util.ArrayList 类中的 contains(Object) 方法,如果该列表中包含指定元素,则返回 true。
示例
import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList<Integer> arrlist = new ArrayList<Integer<(8); arrlist.add(20); arrlist.add(25); arrlist.add(10); arrlist.add(15); for (Integer number : arrlist) { System.out.println("Number = " + number); } boolean retval = arrlist.contains(10); if (retval == true) { System.out.println("element 10 is contained in the list"); } else { System.out.println("element 10 is not contained in the list"); } boolean retval2 = arrlist.contains(30); if (retval2 == true) { System.out.println("element 30 is contained in the list"); } else { System.out.println("element 30 is not contained in the list"); } } }
输出
Number = 20 Number = 25 Number = 10 Number = 15 element 10 is contained in the list element 30 is not contained in the list
广告