search

Home  >  Q&A  >  body text

android - java判断字符串数组中是否存在某个值,arrays类有这个方法吗

java判断字符串数组中是否存在某个值,arrays类有这个方法吗

巴扎黑巴扎黑2769 days ago700

reply all(7)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:39:33

    binarySearchThe string array is required to be in order. If you are not sure whether it is in order, you should write your own judgment
    Just to determine whether a certain value exists, there is no need to sort it first.
    For example:

    public static final String[] TYPES = {
            "Sedan",
            "Compact",
            "Roadster",
            "Minivan",
            "SUV",
            "Convertible",
            "Cargo",
            "Others"
        };
    
    String carName = "SUV"; // 比如说SUV
    int index = -1;
    for (int i=0;i<TYPES.length;i++) {
        if (TYPES[i].equals(carName)) {
            index = i;
            break;
        }
    }
    

    http://stackoverflow.com/questions/23160832/how-to-find-index-of-strin...

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:39:33

    https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#binarySearch(char[],%20char)

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:39:33

    javabinarySearch(char[] a, char key)
    

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:39:33

    If there are not many things and they are disordered... you can write them yourself..
    What they say is that Arrays.binarySearch(..) uses the binary search method, which is faster, but it must be an ordered array. If yours is unordered, you must sort it first...

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:39:33

    This, this, this, the correct answer upstairs. Sorting the string array first in order to use the dichotomy method is simply using the class library for the sake of using the class library. A problem that can be solved by writing a loop. On the top floor.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:39:33

    It is also very convenient to convert the array into a collection such as a list and then judge

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:39:33

    String[] array = { "Sedan", "Compact", "Roadster", "Minivan", "SUV",
        "Convertible", "Cargo", "Others" };
    System.out.println(Arrays.asList(array).contains("SUV"));
    

    reply
    0
  • Cancelreply