PHP中文网2017-04-17 13:39:33
binarySearch
要求字符串数组是有序的,不肯定是否有序的情况下还是自己写判断吧
仅仅是为了判断是否存在某个值,也没有必要首先排序吧。
比如:
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
东西不多并且本来就是无序的话.. 可以自己写..
他们说的Arrays.binarySearch(..) 采用的是2分法查询, 会比较快, 但必须是有序的数组, 如果你的是无序的还得先排序...
天蓬老师2017-04-17 13:39:33
String[] array = { "Sedan", "Compact", "Roadster", "Minivan", "SUV",
"Convertible", "Cargo", "Others" };
System.out.println(Arrays.asList(array).contains("SUV"));