我的问题:有两个数组
double [] a = {0.5, 0.6, 0.3};
double [] b = {0.2, 0.1, 0.9};
而且我还有一些第三数组: 换句话说,我必须找到与其他数组最接近的数组。 所以,我如何在代码中确定这一点? double [] c = {0.1, 0.2, 0.8}
。如我们所见,该数组的各个元素最接近第二个数组-> 0.2-0.1 <0.5-0.1(b [0] -c [0]
如果只想按绝对值查找最接近的数组,则可以使用math.abs()方法获取并比较它们。
public static double[] nearestArray(double[] a,double[] b,double[] c){
double an=0,bn=0;
for(int i =0;i<c.length;i++){
an=an+Math.abs(a[i]-c[i]);
bn=bn+Math.abs(b[i]-c[i]);
}
if(an>bn){
return b;
}else if(bn>an){
return a;
}else{
return null;
}
}