Java Examples - Find a specified element in an array
The following example demonstrates how to use the contains () method to find a specified element in an array:
Main.java file
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> objArray = new ArrayList<String>();
ArrayList<String> objArray2 = new ArrayList<String>();
objArray2.add(0,"common1");
objArray2.add(1,"common2");
objArray2.add(2,"notcommon");
objArray2.add(3,"notcommon1");
objArray.add(0,"common1");
objArray.add(1,"common2");
System.out.println("Array elements of objArray:"+objArray);
System.out.println("Array elements of objArray2:"+objArray2);
System.out.println("Does objArray contain the string common2? :"
+objArray.contains("common2"));
System.out.println("Does objArray2 contain the array objArray? :"
+objArray2.contains(objArray) );
}
}
The output of the above code is:
objArray 的数组元素:[common1, common2] objArray2 的数组元素:[common1, common2, notcommon, notcommon1] objArray 是否包含字符串common2? : true objArray2 是否包含数组 objArray? :falseOther Extensions
Java Examples