import java.util.*;
public class BinarySearch {
public int getPos(int[] A, int n, int val) {
int position=-1; //返回的数组的位置
int low=0,high=n-1,mid=n/2;
if(n<=0||A==null){// 数组为空或者为null
position=-1;
}else{// 数组有元素
while(low<=high){
if(val>A[mid]){
low=mid+1;
mid=(low+high)/2;
}else if(val<A[mid]){
high=mid-1;
mid=(low+high)/2;
}else{
position=mid;
break ; //跳出循环
}
}
}
// 判断position
if(position==-1)
return position;
else{
while(position>=0&&A[position]==val){
position--;
}
return position+1;
}
}
}