在有序数组列表中<Double>查找最接近的双精度



我有一个排序的 ArrayList,我想在其中找到最接近的双x元素。

double x = 50.2;
ArrayList<Double> arrayList = new ArrayList<>();
arrayList.add(12.34);
arrayList.add(86.00);
arrayList.add(87.26);
arrayList.add(241.63);
...
double findNearestDoubleInList(list, x)
{
   ...
}

我该如何做到这一点?

使用 java.lang.Math.abs 获取列表中的值与所需值之间的差异,并找到最接近的值。

    double answer = arrayList.get(0);
    double current = Double.MAX_VALUE;
    for (Double value : arrayList) {
        if (Math.abs(value - x) < current) {
            answer = value;
            current = Math.abs(value - x);
        }
    }

浏览列表并计算与列表中x和当前元素的绝对差值。

返回最小的绝对差值。

static double findNearestDoubleInList(ArrayList<Double> list, double d){
    if(list.size() == 0){
        return -1;
    }
    if(list.size() == 1){
        return list.get(0);
    }
    double current = list.get(0);
    double currentMin = Math.abs(list.get(0) - d);
    for(int i = 1; i < list.size(); i ++){
        double difference = Math.abs(list.get(i) - d);
        if(currentMin > difference){
            currentMin = difference;
            current = list.get(i);
        }
    }
    return current;
}

对于这样的算法,请始终假设第一个元素是您的解决方案。

// Java program to find element closet to given target by binary search on the sorted array

class FindClosestNumber {
    // Returns element closest to target in arr[]
    public static double findClosest(double[] arr, double target)
    {
        int n = arr.length;
        // Corner cases
        if (target <= arr[0])
            return arr[0];
        if (target >= arr[n - 1])
            return arr[n - 1];
        // Doing binary search
        int i = 0, j = n, mid = 0;
        while (i < j) {
            mid = (i + j) / 2;
            // if arr[mid] and target are very close 
            if (Math.abs(arr[mid]-target) < 0.0001)
                return arr[mid];
            /* If target is less than array element,
               then search in left */
            if (target < arr[mid]) {
                // If target is greater than previous
                // to mid, return closest of two
                if (mid > 0 && target > arr[mid - 1])
                    return getClosest(arr[mid - 1],
                            arr[mid], target);
                /* Repeat for left half */
                j = mid;
            }
            // If target is greater than mid
            else {
                if (mid < n-1 && target < arr[mid + 1])
                    return getClosest(arr[mid],
                            arr[mid + 1], target);
                i = mid + 1; // update i
            }
        }
        // Only single element left after search
        return arr[mid];
    }
    // Method to compare which one is the more close
    // We find the closest by taking the difference
    //  between the target and both values. It assumes
    // that val2 is greater than val1 and target lies
    // between these two.
    public static double getClosest(double val1, double val2,
                                 double target)
    {
        if (target - val1 >= val2 - target)
            return val2;
        else
            return val1;
    }
    // Driver code
    public static void main(String[] args)
    {
        double arr[] = {12.34, 86.00, 87.26, 241.63};
        double target = 50.2;
        System.out.println(findClosest(arr, target));
    }
}

最新更新