android - java判断字符串数组中是否存在某个值,arrays类有这个方法吗
巴扎黑
巴扎黑 2017-04-17 13:37:33
0
7
649

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

巴扎黑
巴扎黑

reply all(7)
Ty80

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...

迷茫

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

巴扎黑
javabinarySearch(char[] a, char key)
迷茫

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...

黄舟

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.

Peter_Zhu

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

Peter_Zhu
String[] array = { "Sedan", "Compact", "Roadster", "Minivan", "SUV",
    "Convertible", "Cargo", "Others" };
System.out.println(Arrays.asList(array).contains("SUV"));
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!