Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:搜索数组中最近的值_Java_Arrays_Algorithm - Fatal编程技术网

Java:搜索数组中最近的值

Java:搜索数组中最近的值,java,arrays,algorithm,Java,Arrays,Algorithm,我有一个大维度的有序双精度数组,例如: [2.1,3.4,3.6,4.1] 3.51 现在,我以某种方式生成一个双数,例如: [2.1,3.4,3.6,4.1] 3.51 我需要在Java中创建一个函数来获取数组和数字,并给出这个数字数组中最接近的值。在本例中为3.6 我怎样才能以最有效的方式做到这一点?因为我可以有300000双数组,需要经常做这个操作。那我就不能做一个简单的比较了 编辑: 我做了这个,在一些测试中结果是正确的,对你来说是正确的吗 int pos = A

我有一个大维度的有序双精度数组,例如:

[2.1,3.4,3.6,4.1]
3.51
现在,我以某种方式生成一个双数,例如:

[2.1,3.4,3.6,4.1]
3.51
我需要在Java中创建一个函数来获取数组和数字,并给出这个数字数组中最接近的值。在本例中为3.6

我怎样才能以最有效的方式做到这一点?因为我可以有300000双数组,需要经常做这个操作。那我就不能做一个简单的比较了

编辑: 我做了这个,在一些测试中结果是正确的,对你来说是正确的吗

        int pos = Arrays.binarySearch(allTime.times, value);
        double out;

        if(pos >= 0)
        {
//          System.out.println(allTime.times[pos]);
            out = allTime.times[pos];
        }
        else if(pos == -1)
        {
//          System.out.println(allTime.times[0]);
            out = allTime.times[0];
        }
        else
        {
            int insertionPoint = -pos-1;
            if(insertionPoint < allTime.times.length)
            {
                if(allTime.times[insertionPoint] - value < value - allTime.times[insertionPoint-1])
//                  System.out.println(allTime.times[insertionPoint] );
                    out = allTime.times[insertionPoint];
                else
//                  System.out.println(allTime.times[insertionPoint-1] );
                    out = allTime.times[insertionPoint-1];
            }
            else
//              System.out.println(allTime.times[allTime.times.length -1]);
                out = allTime.times[allTime.times.length -1];
        }
int pos=Arrays.binarySearch(allTime.times,value);
加倍;
如果(位置>=0)
{
//System.out.println(allTime.times[pos]);
out=所有时间。时间[pos];
}
否则如果(位置==-1)
{
//System.out.println(allTime.times[0]);
out=所有时间.次[0];
}
其他的
{
int insertionPoint=-pos-1;
if(插入点
数组。二进制搜索也适用于数组中不包含的元素

int pos = Arrays.binarySearch(arr, value);
如果不包含该值,则返回一个负值,该负值描述了该值应位于的位置:
((插入点)-1)

然后找出两个相邻值中的哪一个是正确的

if(pos < 0)
{
  int insertionPoint = -pos-1;
  if(insertionPoint == arr.length) //value is bigger than every value in array
    //arr[insertionPoint-1] is the nearest value
  else if(insertionPoint == 0) //value is smaller than every value in array
    //arr[0] is the nearest value
  else if(value-arr[insertionPoint-1] < arr[insertionPoint]-value)
    //arr[insertionPoint-1] is the nearest value
  else
    //arr[insertionPoint] is the nearest value
}else
  //arr[pos] has the same value
if(位置<0)
{
int insertionPoint=-pos-1;
if(insertionPoint==arr.length)//值大于数组中的每个值
//arr[insertionPoint-1]是最接近的值
else if(insertionPoint==0)//值小于数组中的每个值
//arr[0]是最接近的值
else if(值arr[insertionPoint-1]

索引
插入点处的值
大于
。我还处理了值包含在数组中的情况。

Jimmy T answer不处理角点情况,比如搜索编号落在第0个索引之前或落在最后一个索引之外

int pos = Arrays.binarySearch(arr, value);
这是正确的

     double searchNearest(double[] array, double searchNumber) {
    int pos = Arrays.binarySearch(array, searchNumber);
    if (pos >= 0)
        return searchNumber;
    else {
        int insertionPoint = -pos - 1;
        if (insertionPoint > 0 && insertionPoint < array.length) {
            if ((searchNumber - array[insertionPoint - 1]) < (array[insertionPoint] - searchNumber)) {
                return array[insertionPoint - 1];
            } else {
                return array[insertionPoint];
            }

        } else {

            return insertionPoint == 0 ? array[0] : array[array.length - 1];
        }
    }

}
double searchNearest(double[]数组,double searchNumber){
int pos=Arrays.binarySearch(数组,searchNumber);
如果(位置>=0)
返回搜索号码;
否则{
int insertionPoint=-pos-1;
if(insertionPoint>0&&insertionPoint
查看TreeSet天花板、地板、上下方法。如果数组较大(未排序),则提供一些GPUIf。如果数组已排序,则应使用二进制搜索O(log n)。如果未排序,则需要读取孔数组O(n)。
插入点不能小于零。如果在上述数据中输入1.1,插入点将为-1。不是吗?
pos
将为-1<代码>插入点
将是
-(-1)-1
,这将导致
0