PHP中文网2017-04-17 13:39:33
binarySearch
The 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...
迷茫2017-04-17 13:39:33
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#binarySearch(char[],%20char)
迷茫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...
黄舟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.
天蓬老师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
天蓬老师2017-04-17 13:39:33
String[] array = { "Sedan", "Compact", "Roadster", "Minivan", "SUV",
"Convertible", "Cargo", "Others" };
System.out.println(Arrays.asList(array).contains("SUV"));