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;
}
}
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.
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 judgmentJust to determine whether a certain value exists, there is no need to sort it first.
For example:
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)
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.
It is also very convenient to convert the array into a collection such as a list and then judge