我正在尝试读取文件并生成2D阵列。所以我相信我的构造函数会创建正确的维度,我只是不知道如何将实际值输入到数组中。如何通过扫描文件创建2D阵列
文件格式:
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];
}
我的想法是创建一个二维数组是该文件的第一行的大小。然后对于外部值,创建四个代表外部的数组。我不知道如何将这些外部数组放入我的二维数组中。
分开跟踪外部阵列比其工作更多的工作。我可能会把它们全部放在一个数组中,让2d数组2在两个方向都变大。 – thermite