2015-04-06 170 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