Home > Java > JavaBase > body text

How to determine whether an array contains an element in java

Release: 2019-12-05 11:53:05
Original
13987 people have browsed it

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(...)
Copy after login

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)
Copy after login

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!