Home  >  Article  >  Java  >  How to determine whether an array contains an element in java

How to determine whether an array contains an element in java

尚
Original
2019-11-23 10:33:2213952browse

How to determine whether an array contains an element in java

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn