There are two methods to determine whether the array contains elements:
Method 1, convert the array to a list, and then use the contains method of the list to determine:
Arrays.asList(...).contains(...)
java.lang.String.contains() method returns true if and only if this string contains the specified char value sequence
Declare the java.lang.String.contains() method:
public boolean contains(CharSequence s)
Return value: This method returns true if this string contains, otherwise returns false.
Method 2, traverse the array to determine:
public static <T> boolean contains( final T[] array, final T v ) { for ( final T e : array ) if ( e == v || v != null && v.equals( e ) ) return true; return false; }
For more java knowledge, please pay attention to java basic tutorial.
The above is the detailed content of How to determine whether an array contains an element in java. For more information, please follow other related articles on the PHP Chinese website!