首页上一页 1 下一页尾页 4 条记录 1/1页
java
发表在Java答疑区
2017-03-10
是否精华
是
否
版块置顶:
是
否
int[] array = { 112 ,114 ,118 ,123 ,132 ,135 ,146 ,155 ,162 ,165 };
System.out.print("请输入待查找元素: ");
int low = 0, suo = 0, high = array.length;
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改为high = array.length-1再运行也没有问题,那么究竟哪个比较好点?