本帖最后由 冷风 于 2014-8-5 11:17 编辑
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at ArrZhaoShu.getIndex(ArrZhaoShu.java:28)
at ArrZhaoShu.main(ArrZhaoShu.java:6)
public static void main(String[] args)
{
int[] arr ={1,3,5,7,9,11,13};
int index=getIndex(arr,5);
System.out.println("index="+index);
}
public static int getIndex(int [] arr,int key)
{
int min, max, mid;
min=0;
max=arr.length;
mid=(min+max)/2;
while (arr[mid]!=key)//这是第28行
{
if(key<arr[mid])
min=mid+1;
else if(key>arr[mid])
max=mid-1;
if(min>max)
return -1;
mid=(min+max)/2;
}
return mid;
}
我看了半天我也没找到我到底是那写错了:'( |
|