因此,我需要使用类org.apache.commons.math3.stat.StatUtils来计算浮点数数组的平均值,但是输入数组应该是一个双数组。
如何将float[]转换为double[]
发布于 2014-10-14 11:08:15
您可以通过定义逻辑步骤来解决您的问题,这些步骤共同实现了目标:
double[]相同长度的float[]float[],并在double[]中相应的索引中逐个保存值传统上,或pre 8之一都是这样的:
final double[] doubleArray = new double[floatArray.length];
for (int i = 0; i < floatArray.length; i++) {
doubleArray[i] = floatArray[i]; // no casting needed
}当使用Java 8时,您可以使用流API来获得更整洁的解决方案:
IntStream.range(0, floatArray.length).forEach(index -> doubleArray[index] = floatArray[index]);作为职能的可能用途:
public static double[] convertToDouble(final float[] values) {
final double[] result = new double[values.length];
IntStream.range(0, values.length).forEach(index -> result[index] = values[index]);
return result;
}发布于 2014-10-14 10:01:51
将数组中的每个值转换为双值。浮动比双倍短,所以你不必担心失去精度。
创建与浮点数相同长度的双数组。然后,您可以将浮点数数组内嵌到双数组。
public double[] answer(float[] array){
double[] newArray = new double[array.length];
for ( int i = 0 ; i < array.length ; i++ )
newArray[i] = (double) array[i];
return newArray;
}https://stackoverflow.com/questions/26357842
复制相似问题