Java determines whether an element is inside an array

Source: Internet
Author: User

First, go to list, then use the Contains () method
    New String[] {"A", "B", "C"};     = "C";    List<String> list = arrays.aslist (strarr);     Boolean result = list.contains (str);     // true

Second, use the most basic for loop

The For loop method is the most efficient

    New String[] {"A", "B", "C" };     = "C";      for (int i = 0; i < strarr.length; i++) {        if  (Strarr[i].equals (str)) {            SYSTEM.OUT.PRINTLN (//  The element in the array: i=2        }    }

Third, use Apache Commons's Arrayutils

The Apache Commons Class library has a lot, and almost most of the open source framework relies on it, and the tools in Commons save you most of the time, it contains some common static methods and Java extensions. is a set of frameworks for improving efficiency in development.

    New String[] {"A", "B", "C" };     = "C";     Boolean result =//Recommended     //  true

https://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/

Java determines whether an element is inside an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.