2015-04-06 253 views
0

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



} 

我的想法是創建一個二維數組是該文件的第一行的大小。然後對於外部值,創建四個代表外部的數組。我不知道如何將這些外部數組放入我的二維數組中。

+1

分開跟蹤外部陣列比其工作更多的工作。我可能會把它們全部放在一個數組中,讓2d數組2在兩個方向都變大。 – thermite

回答

1

與所有文件讀取一樣,嘗試將每個任務分開的任務分開。問問自己:「在我做什麼之前我需要知道什麼,爲了完成我需要做些什麼?」希望這些任務按順序列出(每個任務只需要文件中的信息),這是您的問題。

你的任務似乎涉及三個子任務:

  1. 圖如何大的陣列和矩陣必須是(1號線)
  2. 閱讀在側陣列(4號線)
  3. 閱讀在矩陣(N線)

讓我們與合作:

int[][] board; 
int dim; 
int[] NS, SN, EW, WE; //the outter arrays 

public SkyscraperConfig(Scanner f){ 
    //First line should be dimension line 
    int dim = Integer.parseInt(f.nextLine()); 

    //Initalize data structures based off of this dimension 
    this.NS = new int[dim]; 
    this.SN = new int[dim]; 
    this.EW = new int[dim]; 
    this.WE = new int[dim]; 
    this.board = new int[dim][dim]; 

    //Read in side arrays 
    //... 

    //Read in the board 
    //... 
} 

在這裏,我們可以猜測,我們將在閱讀這些行時會有很多重複的代碼 - 可能是開始設計幫助程序方法的時候了。我們似乎正在做的一件事是閱讀一行並解析其中的所有內容。因此,讓我們寫的是

private static int[] parseIntLine(String line){ 
    String[] arr = line.trim().split(' '); //Split on the space character 
    int[] intArr = new int[arr.length]; 
    for(int i = 0; i < arr.length; i++){ 
    intArr[i] = Integer.parseInt(arr[i]); 
    } 
    return intArr; 
} 

的方法現在我們可以用這個方法來完成了我們的實現,讓閱讀照顧數組長度:

public SkyscraperConfig(Scanner f){ 
    //First line should be dimension line 
    int dim = Integer.parseInt(f.nextLine()); 

    //Only actual initialization we have to do is for the matrix's outer array 
    board = new int[dim][]; 

    //Read in side arrays 
    NS = parseIntLine(f.nextLine()); 
    EW = parseIntLine(f.nextLine()); 
    SN = parseIntLine(f.nextLine()); 
    WE = parseIntLine(f.nextLine()); 

    //Read in the board - dim rows to read 
    for(int i = 0; i < dim; i++){ 
    board[i] = parseIntLine(f.nextLine()); 
    } 

    f.close(); 
} 

現在有很多的東西,可能會出錯,你應該考慮。如果第一行不只包含一個數字會怎樣?如果第一行包含非整數值會怎麼樣?如果其中一個側面陣列或其中一個板排的長度錯誤,該怎麼辦?如果沒有足夠的行填充董事會怎麼辦?如果行數太多會怎樣?對於這些問題中的每一個,您都應該在方法內處理案例(通過try/catch或if-else邏輯),或者如果這是一個不可修復的問題,則拋出某種異常。

+0

感謝您的詳細解答。只是一個簡單的問題,但。我怎樣才能打印出我的二維數組? – FatFockFrank

+0

當我嘗試打印配置時,我得到'SkyscraperConfig @ 4fca772d' – FatFockFrank

+1

Chck out Arrays.deepToString(...) – Mshnik