【发布时间】:2015-04-06 17:51:15
【问题描述】:
我正在尝试读取文件并生成二维数组。所以我相信我的构造函数会创建正确的维度,我只是不知道如何将实际值输入到数组中。
文件格式:
6
1 4 2 2 2 3
4 2 2 4 1 2
2 1 3 4 3 2
1 3 3 2 6 2
0 0 0 2 0 0
3 4 0 0 0 0
0 0 0 1 0 0
0 1 0 0 0 0
0 0 0 0 0 6
5 0 1 0 0 4
文件输入在左边,板子结果应该像右边:
6 | 1 4 2 2 2 3
1 4 2 2 2 3 | -----------
4 2 2 4 1 2 | 1|. . . 2 . .|4
2 1 3 4 3 2 | 3|3 4 . . . .|2
1 3 3 2 6 2 | 3|. . . 1 . .|2
0 0 0 2 0 0 | 2|. 1 . . . .|4
3 4 0 0 0 0 | 6|. . . . . 6|1
0 0 0 1 0 0 | 2|5 . 1 . . 4|2
0 1 0 0 0 0 | -----------
0 0 0 0 0 6 | 2 1 3 4 3 2
5 0 1 0 0 4 |
文件的第一行是棋盘的大小 (6x6)。
第二行是“北到南(NS)”的面值
第三行是“东到西(EW)”的面值
第四行是“南到北(SN)”的面值
第五行是“西到东(WE)”的面值。
其余的行将填充板。 0 不会放任何东西。
public static final int EMPTY = 0;
int[][] board;
int dim;
int[] NS, SN, EW, WE; //the outter arrays
public SkyscraperConfig(Scanner f){
while(f.hasNextLine()){
if(f.nextLine().length() == 1){
dim = f.nextInt();
}
else{
outterArrays = f.nextLine().length();
}
}
this.board = new int[dimension+1][dimension+1];//I made the dimension +1 to hold the outter arrays that hold the NS, SN, EW, and WE values
this.NS = new int[outterArrays+1];
this.SN = new int[outterArrays+1];
this.EW = new int[outterArrays+1];
this.WE = new int[outterArrays+1];
}
我的想法是创建一个与文件中第一行大小相同的二维数组。然后对于外部值,创建四个代表外部的数组。我不知道如何将这些外部数组放入我的二维数组中。
【问题讨论】:
-
单独跟踪外部数组比它的工作更多。我可能会将它们全部放在一个数组中,只是使二维数组 2 在两个方向上都更大。
标签: java multidimensional-array java.util.scanner