search
HomeJavaJavaBaseJava determines whether an array contains a certain value

Java determines whether an array contains a certain value

java determines whether an array contains a certain value

I believe you often check when operating Java Does an array (unordered) contain a specific value? This is a frequently used and very useful operation in Java.

Four methods are given below. The most efficient one is the loop method. If you are interested, you can test it:

public boolean findStr(String[] args,String str){
        boolean result = false;
        //第一种:List
        result = Arrays.asList(args).contains(str);
        //第二种:set
        Set<String> sets = new HashSet<String>(Arrays.asList(args));
        result = sets.contains(str);
        //第三种:loop
        for (String s : args) {
            if (s.equals(str)){
                return true;
            }
        }
        //第四种:binarySearch(Arrays的binarySearch方法必须应用于有序数组)
        int res = Arrays.binarySearch(args, str);
        if (res > 0){
            return true;
        }
         
        return result;
}

The Arrays.binarySearch method has limitations and must be applied to ordered arrays. . It is recommended to use a loop to judge, which is highly efficient.

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of Java determines whether an array contains a certain value. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version