如何将包含双精度的字符串转换为 java 中的矩阵
How to convert a string that contains doubles into a matrix in java
String a = "0 2 4 1 4.5 2.2 1.1 4.3 5.2";
这里我们有一个由 9 个数字组成的字符串,其中一些是 double,一些是 int。我需要将它转换成一个矩阵,当我在字符串中只给出整数值时,这很容易,因为在我为每个字符串值是一个数字的 parseInt 之后,我所做的一切都在整个整数上进行双倍转换。
例如,
String a = "1 2 3 4 5 6 7 8 9";
double[][] matrixA = new double[3][3];
int counter = 0;
for (int i = 0; i <= a.length(); i++) {
char c = a.charAt(i);
if (c == ' ') {
counter = counter;
} else {
counter++;
double k = Double.parseDouble(String.valueOf(c));
if (counter <= 3) {
matrixA[0][counter - 1] = k;
} else if (counter > 3 && counter <= 6) {
matrixA[1][counter - 4] = k;
} else {
matrixA[2][counter - 7] = k;
}
}
}
这给了我一个从 1 到 9 的 3×3 矩阵。但是,当我得到一个包含实数或 'doubles' 的字符串时,我的方法无法创建矩阵,因为它计算字符串的每个元素,其中一个是句点。那么,当提供给我的字符串包含双精度值时,如何创建一个 3×3 矩阵?
这应该可以解决双打和负数的问题。注意从矩阵坐标到索引的转换 index = x + y * width
String a = "1 2 -3 4.5 5 6.5 7.5 8 9";
double[][] matrix = new double[3][3];
String[] nums = a.split(" ");
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length; j++){
matrix[i][j] = Double.parseDouble(nums[matrix[j].length * i + j]);
}
}
很好,尽管我会用简单的 "next value" 索引替换输入索引的计算。
String a = "1 2 -3 4.5 5 6.5 7.5 8 9";
double[][] matrix = new double[3][3];
String[] nums = a.split(" ");
for (int i = 0, k = 0; i < matrix.length; i++) { // <== Initialize k here
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = Double.parseDouble(nums[k++]); // <== Simpler code
}
}
System.out.println(Arrays.deepToString(matrix));
输出
[[1.0, 2.0, -3.0], [4.5, 5.0, 6.5], [7.5, 8.0, 9.0]]
锯齿状数组
即使二维数组是 jagged。
String a = "1 2 -3 4.5 5 6.5 7.5 8 9";
double[][] matrix = new double[][] { new double[2], new double[3], new double[4] }; // <== Jagged array
String[] nums = a.split(" ");
for (int i = 0, k = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = Double.parseDouble(nums[k++]);
}
}
System.out.println(Arrays.deepToString(matrix));
输出
[[1.0, 2.0], [-3.0, 4.5, 5.0], [6.5, 7.5, 8.0, 9.0]]
扫描器
或者,代替 split()
和 k++
逻辑,使用 Scanner
.
String a = "1 2 -3 4.5 5 6.5 7.5 8 9";
double[][] matrix = new double[3][3];
try (Scanner sc = new Scanner(a)) { // <== Use Scanner
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = sc.nextDouble(); // <== Use Scanner
}
}
}
System.out.println(Arrays.deepToString(matrix));
输出
[[1.0, 2.0, -3.0], [4.5, 5.0, 6.5], [7.5, 8.0, 9.0]]
您可以split
this string into an array, then parseDouble
子字符串中的值,然后收集一个新的二维数组,其中包含指定长度的行,如下所示:
String a = "0 2 4 1 4.5 -2.2 1.1 4.3 5.2";
// array of numbers as strings
String[] b = a.split(" ");
// length of the row
int m = 3;
// 2d array with rows of a specified
// length, or less if not possible
double[][] dd = IntStream
// iterate over the indices of
// the string array with step 'm'
.iterate(0, i -> i < b.length, i -> i + m)
// subarray of a specified length at each step
.mapToObj(i -> Arrays
// substream with a specified
// range, or less if not possible
.stream(b, i, Math.min(i + m, b.length))
// string to double
.mapToDouble(Double::parseDouble)
.toArray())
.toArray(double[][]::new);
// output
Arrays.stream(dd).map(Arrays::toString).forEach(System.out::println);
//[0.0, 2.0, 4.0]
//[1.0, 4.5, -2.2]
//[1.1, 4.3, 5.2]
另请参阅:
String a = "0 2 4 1 4.5 2.2 1.1 4.3 5.2";
这里我们有一个由 9 个数字组成的字符串,其中一些是 double,一些是 int。我需要将它转换成一个矩阵,当我在字符串中只给出整数值时,这很容易,因为在我为每个字符串值是一个数字的 parseInt 之后,我所做的一切都在整个整数上进行双倍转换。
例如,
String a = "1 2 3 4 5 6 7 8 9";
double[][] matrixA = new double[3][3];
int counter = 0;
for (int i = 0; i <= a.length(); i++) {
char c = a.charAt(i);
if (c == ' ') {
counter = counter;
} else {
counter++;
double k = Double.parseDouble(String.valueOf(c));
if (counter <= 3) {
matrixA[0][counter - 1] = k;
} else if (counter > 3 && counter <= 6) {
matrixA[1][counter - 4] = k;
} else {
matrixA[2][counter - 7] = k;
}
}
}
这给了我一个从 1 到 9 的 3×3 矩阵。但是,当我得到一个包含实数或 'doubles' 的字符串时,我的方法无法创建矩阵,因为它计算字符串的每个元素,其中一个是句点。那么,当提供给我的字符串包含双精度值时,如何创建一个 3×3 矩阵?
这应该可以解决双打和负数的问题。注意从矩阵坐标到索引的转换 index = x + y * width
String a = "1 2 -3 4.5 5 6.5 7.5 8 9";
double[][] matrix = new double[3][3];
String[] nums = a.split(" ");
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length; j++){
matrix[i][j] = Double.parseDouble(nums[matrix[j].length * i + j]);
}
}
String a = "1 2 -3 4.5 5 6.5 7.5 8 9";
double[][] matrix = new double[3][3];
String[] nums = a.split(" ");
for (int i = 0, k = 0; i < matrix.length; i++) { // <== Initialize k here
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = Double.parseDouble(nums[k++]); // <== Simpler code
}
}
System.out.println(Arrays.deepToString(matrix));
输出
[[1.0, 2.0, -3.0], [4.5, 5.0, 6.5], [7.5, 8.0, 9.0]]
锯齿状数组
即使二维数组是 jagged。
String a = "1 2 -3 4.5 5 6.5 7.5 8 9";
double[][] matrix = new double[][] { new double[2], new double[3], new double[4] }; // <== Jagged array
String[] nums = a.split(" ");
for (int i = 0, k = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = Double.parseDouble(nums[k++]);
}
}
System.out.println(Arrays.deepToString(matrix));
输出
[[1.0, 2.0], [-3.0, 4.5, 5.0], [6.5, 7.5, 8.0, 9.0]]
扫描器
或者,代替 split()
和 k++
逻辑,使用 Scanner
.
String a = "1 2 -3 4.5 5 6.5 7.5 8 9";
double[][] matrix = new double[3][3];
try (Scanner sc = new Scanner(a)) { // <== Use Scanner
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = sc.nextDouble(); // <== Use Scanner
}
}
}
System.out.println(Arrays.deepToString(matrix));
输出
[[1.0, 2.0, -3.0], [4.5, 5.0, 6.5], [7.5, 8.0, 9.0]]
您可以split
this string into an array, then parseDouble
子字符串中的值,然后收集一个新的二维数组,其中包含指定长度的行,如下所示:
String a = "0 2 4 1 4.5 -2.2 1.1 4.3 5.2";
// array of numbers as strings
String[] b = a.split(" ");
// length of the row
int m = 3;
// 2d array with rows of a specified
// length, or less if not possible
double[][] dd = IntStream
// iterate over the indices of
// the string array with step 'm'
.iterate(0, i -> i < b.length, i -> i + m)
// subarray of a specified length at each step
.mapToObj(i -> Arrays
// substream with a specified
// range, or less if not possible
.stream(b, i, Math.min(i + m, b.length))
// string to double
.mapToDouble(Double::parseDouble)
.toArray())
.toArray(double[][]::new);
// output
Arrays.stream(dd).map(Arrays::toString).forEach(System.out::println);
//[0.0, 2.0, 4.0]
//[1.0, 4.5, -2.2]
//[1.1, 4.3, 5.2]
另请参阅: