复制代码
- class ArrayTest4
- {
- public static void main(String[] args)
- {
- int[] arr = {2,4,5,7,8,19,32,45};//8
- int index = getIndex_2(arr,190);
- System.out.println("index="+index);}
- public static int getIndex_2(int[] arr,int key)
- {
- int min = 0,max = arr.length-1,mid;
- while(min<=max)
- {
- mid = (max+min)>>1;
- if(key>arr[mid])
- min = mid + 1;
- else if(key<arr[mid])
- max = mid - 1;
- else
- return mid;
- }
- return min;
- }
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |