【发布时间】:2018-07-12 14:41:56
【问题描述】:
我被要求读取一个文件并将其中的文本转换为二维数组。由于 Eclipse 很痛苦并且不会打开/读取我的文本文件,因此我在使用单独初始化的 2d 数组的类中进行了测试。我的问题是我不知道将 parseInt'ed 数组放回新数组中。或者如何使用它来形成一个新的二维数组。这是我的代码: public static void main(String[] args) {
String[][] tester = new String[][] { { "-1 2 3 0" }, { "-1 3 4 0" }, { "-1 3 -4 0" }, { "-1 -3 4 0" } };
int row = 0;
int col = 0;
int count = 0;
int[][] formula = new int[4][];
while (count < 4) {
String temp = tester[row][col];
String[] charArray = temp.split("\\s+");
int[] line = new int[charArray.length];
for (int i = 0; i < charArray.length; i++) {
String numAsStr = charArray[i];
line[i] = Integer.parseInt(numAsStr);
//what to do here??
}
row++;
count++;
}
System.out.println(Arrays.deepToString(formula).replace("], ",
"]\n"));
}
}
我想生成一个如下所示的数组:
-1 2 3 0
-1 3 4 0
-1 3 -4 0
-1 -3 4 0
我怎样才能做到这一点?
【问题讨论】:
-
嗨,这样做
formula[count] = line; -
天哪,我好密集...我不敢相信我错过了这个...谢谢!!!!
标签: java arrays algorithm multidimensional-array string-parsing