使用Java 8流将多维字符串数组(String[][])转换为多维双精度数组(double[][])的方法如下:
完整的示例代码如下:
import java.util.Arrays;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
String[][] stringArray = {{"1.0", "2.0", "3.0"}, {"4.0", "5.0", "6.0"}};
String[] flattenedArray = Arrays.stream(stringArray)
.flatMap(Arrays::stream)
.toArray(String[]::new);
double[] doubleArray = Arrays.stream(flattenedArray)
.mapToDouble(Double::parseDouble)
.toArray();
int rows = stringArray.length;
int columns = stringArray[0].length;
double[][] result = new double[rows][columns];
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = doubleArray[index++];
}
}
System.out.println(Arrays.deepToString(result));
}
}
以上代码将多维字符串数组(String[][])转换为多维双精度数组(double[][]),并输出结果。
领取专属 10元无门槛券
手把手带您无忧上云