我是自己写的,我知道没完善但是也应该可以求出来的啊求解。
package com.itheima.Day05;
public class ArrayFun {
public static void main(String[] args) {
int[]arr={3,6,8,12,19,36,444};
System.out.println(getIndex(arr,444));
}
public static int getIndex(int[]arr,int key){
int min=0;
int max=arr.length-1;
int mid= (min+max)/2;
while(min<=max){
if(key>arr[mid])
min=mid+1;
if(key<arr[mid])
max=mid-1;
if(key==arr[mid])
return mid;
}
return -1;
}
}
|
|