我正在嘗試讀取文件並生成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