【发布时间】:2019-03-08 15:31:50
【问题描述】:
我有以下问题: 给出了我的矩阵每一行的值,列用空格分隔 - 所以我在字符串数组中输入所有行值,删除空格并将数字解析为一个 int 数组。现在每一行的值看起来像 1 个数字“12345”,而它们应该是“1 2 3 4 5”。
如何首先分隔数字,然后通过将元素添加到每一行来填充我的矩阵?谢谢! 这是我的代码:
String n1 = input.nextLine ();
int n = Integer.parseInt(n1); //rows of the matrix
String[] arr = new String [n]; //contains all the rows of the matrix
int [] array = new int [arr.length]; // contains all the elements of the rows of the matrix without whitespace
for (int i = 0; i < arr.length; i++) {
arr [i] = input.nextLine().replaceAll("\\s+","");
array[i] = Integer.parseInt(arr[i]);
}
int matrix [][] = new int [n][arr[0].length()];
【问题讨论】:
-
您的描述与您的代码不符。
标签: java arrays matrix multidimensional-array