从一个数组中查找和目标数最接近的或者相等的数

/**
 * 查找出最接近目标值的数,并返回。
 * @param array
 * @param targetNum
 * @return
 */
	public static int binarysearchKey(Object[] array, int targetNum) {

		//Object[] array = temp.clone();
		Arrays.sort(array);
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		int targetindex = 0;
		int left = 0,right = 0; 
		for (right = array.length-1; left!=right;) {
		int midIndex = (right + left)/2;
		int mid = (right - left);
		int midValue = (Integer) array[midIndex];
		if (targetNum == midValue) {
			return midIndex;
		}

		   if(targetNum > midVal

你可能感兴趣的:(java)