求解答
发表在Java答疑区
2017-02-28
是否精华
是
否
版块置顶:
是
否
int[] array = { 112 ,114 ,118 ,123 ,132 ,135 ,146 ,155 ,162 ,165 };
System.out.print("请输入待查找元素: ");
int low = 0, suo = 0, high = array.length-1 ;
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
while (low <= high) {
suo = (low + high) / 2;
if (array[suo] < x)
low = suo + 1;
else
high = suo - 1;
if (array[suo] == x) {
System.out.println("此元素在数组中的索引为::" + suo);
}
}上面是源代码,我发现将high = array.length-1 ;改为high=array.length也可以运行,那么两者有什么具体区别?此外,如果将int low = 0, suo = 0, high = array.length-1 ;改为int low = 1, suo = 0, high = array.length ;除了不能输出第一个元素的索引外,其余都能输出,那么为什么不能输出第一个元素的索引呢?
首页上一页 1 下一页尾页 4 条记录 1/1页