它打印出 -1 我可能不知道这是怎么发生的,因为“德国”肯定在数组中
public class A
{
static PrintWriter pw = new PrintWriter(System.out, true);
public static void main(String[] args) throws IOException
{
String[] a = new String[4];
a[0]="India";
a[1]="Italy";
a[2]="Germany";
a[3]="India";
pw.println(Arrays.binarySearch(a, "Germany"));
}
}
最佳答案
二分查找只适用于有序数组。
关于java - 为什么即使元素在数组中,此二进制搜索也会返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14131896/