简单的问题,我该怎么把一个txt文件读入到一个二维数组中呢?我需要将每一行放入一个数组中以便以后用于测量长度和宽度的总和,在“生命游戏”中使用。
代码
public P4_Icel_Murad_Life(String fileName) {
Scanner in;
try {
Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
int matrix[][] = new int[size][size];
while (s.hasNext()) {
for (int i = 0; i < matrix.length; i++) {
for (int col = 0; col < matrix.length; col++) {
// matrix[i][col] = ;
}
}
}
s.close();
} catch (IOException i) {
System.out.println("Problems..");
}
}
文本文档
20 20
0 2
0 6
0 7
0 10
0 11
0 16
1 0
1 4
1 7
2 6
2 12
2 15
2 19
3 0
3 4
3 14
3 16
3 17
4 0
4 8
4 12
4 14
4 15
5 0
5 2
5 4
5 7
5 13
5 17
6 11
6 14
6 15
6 18
7 4
7 10
7 13
7 14
8 6
8 11
8 12
8 14
8 17
8 19
9 6
9 8
9 13
9 14
10 3
10 10
10 19
11 6
11 7
11 9
11 10
11 17
12 0
12 2
12 12
13 0
13 1
13 7
13 8
13 14
13 15
14 7
14 9
14 12
14 13
14 16
14 18
15 0
15 2
15 6
15 8
15 10
15 11
15 16
15 17
16 2
16 3
16 18
17 2
17 7
17 11
17 13
17 19
18 0
18 5
18 6
18 11
18 12
18 13
18 14
19 3
19 4
19 6
19 8
19 11
19 16
19 17