1 public class Test2 { 2 static int Ef(int[] arr,int ed){ 3 int left = 0; 4 int right = arr.length-1; 5 while(left <= right){ 6 int tmp = (left+right)/2; 7 if(arr[tmp] > ed){ 8 right = tmp-1; 9 }else if(arr[tmp] < ed){ 10 left = tmp + 1; 11 }else{ 12 return tmp; 13 } 14 } 15 return -1; 16 } 17 public static void main(String[] args) { 18 int[] arr = {1,2,3,4,8,10,59}; 19 int ed = 59; //这是想要找下标的数字 20 int back = Ef(arr, ed); 21 if(back==-1){ 22 System.out.println("数组中不存在该数"); 23 }else{ 24 System.out.println(ed+"的下标:"+back); 25 } 26 27 } 28 }