在有序ArrayList <double>中查找最近的double

时间:2018-01-20 16:04:13

标签: java sorting arraylist

我有一个已排序的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)
{
   ...
}

我如何做到这一点?

3 个答案:

答案 0 :(得分:2)

浏览列表并使用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;
}

对于像这样的算法,总是假设第一个元素是你的解决方案。

答案 1 :(得分:2)

使用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);
        }
    }

答案 2 :(得分:0)

// 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));
    }
}