Java 查找数组中最接近的较高和较低的数字

Java 查找数组中最接近的较高和较低的数字,java,arrays,sorting,compare,Java,Arrays,Sorting,Compare,嗨 实际上,我正在尝试实现一个以整数作为输入的函数。 我还有一个递增整数数组 现在,我试图找到与我的单个整数最接近的下限和上限 我喜欢将其作为数组返回,但我只找到了一个解决方案,可以找到与给定输入最接近的数字 public int getClosestTimeValue(int-time){ int最近值=-1; int bestDistanceFoundYet=Integer.getInteger(null); int[]数组=null; //我们在数组上迭代。。。 for(int i=0;i

实际上,我正在尝试实现一个以整数作为输入的函数。 我还有一个递增整数数组

现在,我试图找到与我的单个整数最接近的下限和上限

我喜欢将其作为数组返回,但我只找到了一个解决方案,可以找到与给定输入最接近的数字

public int getClosestTimeValue(int-time){
int最近值=-1;
int bestDistanceFoundYet=Integer.getInteger(null);
int[]数组=null;
//我们在数组上迭代。。。
for(int i=0;i
有人知道我如何用java解决这个问题吗


谢谢你,卢卡斯现在你只搜索了一次。要找到最近的较低时间和最近的较高时间,您应该有两个变量。然后可以检查迭代时间是否低于或高于输入,并将值存储在相应的变量中。同样,现在您只返回一个值,但是为了返回多个值,您应该通过数组来执行

我不确定它是否回答了你的问题,但以下是我将如何解决这个问题:

array = new int[]; // Array of times you have declared elsewhere.

// Method which returns the array of found times.
public int[] getClosestTime(int time) {
    int closestLowerTime = 0;
    int closestHigherTime = 100; // Value bigger than the largest value in the array.
    times = new int[2]; // Array for keeping the two closest values.
    // Iterating the array.
    for (int i = 0; i < array.length; i++) {
        // Finding the two closest values.
        int difference = time - array[i];
        if (difference > 0 && array[i] > closestLowerTime) {
            closestLowerTime = array[i];
        } else if (difference < 0 && array[i] < closestHigherTime) {
            closestHigherTime = array[i];
        }
    }
    times[0] = closestLowerTime;
    times[1] = closestHigherTime;
    return times;
}
array=newint[];//在别处声明的时间数组。
//方法,该方法返回找到的时间数组。
公共int[]getClosestTime(int-time){
int closestLowerTime=0;
int closestHigherTime=100;//大于数组中最大值的值。
times=new int[2];//用于保留两个最近值的数组。
//迭代数组。
for(int i=0;i0&&array[i]>closestLowerTime){
closestLowerTime=数组[i];
}else if(差<0&&array[i]

这将查找最接近的较低值和较高值,并将它们作为数组返回。当时我解决了这个问题,因为时间在0到100之间,但是如果您不知道最大的时间值,您可以通过另一个循环找到它,该循环迭代数组,并将最大的值存储在
closestHigherTime
中。我没有找到通过数组返回精确值的正确方法,但它是必需的吗?

目前您只搜索了一次。要找到最近的较低时间和最近的较高时间,您应该有两个变量。然后可以检查迭代时间是否低于或高于输入,并将值存储在相应的变量中。同样,现在您只返回一个值,但是为了返回多个值,您应该通过数组来执行

我不确定它是否回答了你的问题,但以下是我将如何解决这个问题:

array = new int[]; // Array of times you have declared elsewhere.

// Method which returns the array of found times.
public int[] getClosestTime(int time) {
    int closestLowerTime = 0;
    int closestHigherTime = 100; // Value bigger than the largest value in the array.
    times = new int[2]; // Array for keeping the two closest values.
    // Iterating the array.
    for (int i = 0; i < array.length; i++) {
        // Finding the two closest values.
        int difference = time - array[i];
        if (difference > 0 && array[i] > closestLowerTime) {
            closestLowerTime = array[i];
        } else if (difference < 0 && array[i] < closestHigherTime) {
            closestHigherTime = array[i];
        }
    }
    times[0] = closestLowerTime;
    times[1] = closestHigherTime;
    return times;
}
array=newint[];//在别处声明的时间数组。
//方法,该方法返回找到的时间数组。
公共int[]getClosestTime(int-time){
int closestLowerTime=0;
int closestHigherTime=100;//大于数组中最大值的值。
times=new int[2];//用于保留两个最近值的数组。
//迭代数组。
for(int i=0;i0&&array[i]>closestLowerTime){
closestLowerTime=数组[i];
}else if(差<0&&array[i]

这将查找最接近的较低值和较高值,并将它们作为数组返回。当时我解决了这个问题,因为时间在0到100之间,但是如果您不知道最大的时间值,您可以通过另一个循环找到它,该循环迭代数组,并将最大的值存储在
closestHigherTime
中。我没有找到通过数组返回精确值的正确方法,但它是必需的吗?

目前您只搜索了一次。要找到最近的较低时间和最近的较高时间,您应该有两个变量。然后可以检查迭代时间是否低于或高于输入,并将值存储在相应的变量中。同样,现在您只返回一个值,但是为了返回多个值,您应该通过数组来执行

我不确定它是否回答了你的问题,但以下是我将如何解决这个问题:

array = new int[]; // Array of times you have declared elsewhere.

// Method which returns the array of found times.
public int[] getClosestTime(int time) {
    int closestLowerTime = 0;
    int closestHigherTime = 100; // Value bigger than the largest value in the array.
    times = new int[2]; // Array for keeping the two closest values.
    // Iterating the array.
    for (int i = 0; i < array.length; i++) {
        // Finding the two closest values.
        int difference = time - array[i];
        if (difference > 0 && array[i] > closestLowerTime) {
            closestLowerTime = array[i];
        } else if (difference < 0 && array[i] < closestHigherTime) {
            closestHigherTime = array[i];
        }
    }
    times[0] = closestLowerTime;
    times[1] = closestHigherTime;
    return times;
}
array=newint[];//在别处声明的时间数组。
//方法,该方法返回找到的时间数组。
公共int[]getClosestTime(int-time){
int closestLowerTime=0;
int closestHigherTime=100;//大于数组中最大值的值。
times=new int[2];//用于保留两个最近值的数组。
//迭代数组。
for(int i=0;i0&&array[i]>closestLowerTime){
closestLowerTime=数组[i];
}else if(差<0&&array[i]
这将查找最接近的较低值和较高值,并将它们作为数组返回。当时我解决了这个问题,因为时间在0到100之间,但万一你不知道最大的时间va