Arrays Find
# Java Example - Find Specified Element in Array
[ Java Example](#)
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){ArrayListobjArray = new ArrayList(); ArrayListobjArray2 = new ArrayList(); 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("Elements of objArrayοΌ"+objArray); System.out.println("Elements of objArray2οΌ"+objArray2); System.out.println("Does objArray contain string common2? οΌ " +objArray.contains("common2")); System.out.println("Does objArray2 contain array objArray? οΌ" +objArray2.contains(objArray)); }}
The output of the above code is:
Elements of objArrayοΌ[common1, common2] Elements of objArray2οΌ[common1, common2, notcommon, notcommon1] Does objArray contain string common2? οΌ true Does objArray2 contain array objArray? οΌ false
YouTip