【发布时间】:2019-12-14 07:34:49
【问题描述】:
我已将一个包含不同长度行的整数的 .txt 文件读入一个数组。我现在需要将这些行细分为单独的数组或二维数组。 .txt 文件的行长可能会发生变化,但行数不会改变。考虑到行长可能因文件而异,我愿意接受有关如何将文件读入锯齿状数组的建议。
.txt 文件如下所示:
4 6 4 2 1 6 1 6 1 6 1
7 0 1 2 0 3 0 4 2 3 0 3 2 0 1 2 0 1 7 0 1
1 2 3 4 1 2 5 1 2 3 4 5
1 2 1 3 1 8 1 1 2 5 4 3 4 7 0 7 1 0 1 2 1 2 7 1 2
5 1 3 1 4 5 1 3 5 2 2 3 4 5 4 2 1 4 1 2 3
这是我的代码:
1 import java.io.BufferedReader;
2 import java.io.FileReader;
3 import java.io.IOException;
4
5
6 public class FileIn{
7
8
9 public static int [] [] PageList = new int [5] [];
10
11 public static int [] Pages;
12
13
14 public static void FileImport() throws IOException {
15
16
17 BufferedReader p = new BufferedReader(new FileReader("reference_strings_0010.txt"));
18
19 String Line;
20
21 while ((Line = p.readLine()) != null) {
22
23 Lines.add(Line);
24 String [] IntLine = Line.split(" ");
25 int [] temp = new int [IntLine.length];
26
27 for (int i = 0; i < temp.length; i++) {
28
29 temp [i] = Integer.parseInt(IntLine[i]);
30 Pages = new int [temp.length];
31 Pages = temp;
32 }
33
34 }
35
36 p.close();
37
38 }
目前,.txt 文件以这种格式读入数组:
[4 6 4 2 1 6 1 6 1 6 1, 7 0 1 2 0 3 0 4 2 3 0 3 2 0 1 2 0 1 7 0 1, 1 2 3 4 1 2 5 1 2 3 4 5, 1 2 1 3 1 8 1 1 2 5 4 3 4 7 0 7 1 0 1 2 1 2 7 1 2, 5 1 3 1 4 5 1 3 5 2 2 3 4 5 4 2 1 4 1 2 3]
我想让数组中的每个整数都是它自己的元素,并且每个数组都应该在逗号所在的地方分开。
非常感谢任何和所有帮助。
【问题讨论】:
-
Xell,将每个 1D 数组 temp 存储在 2D 数组 PageList 中。请尊重 Java 命名约定。删除没有任何用途的页面。
标签: java arrays arraylist integer jagged-arrays