如何通过扫描文件创建二维数组

How to create a 2D array by scanning a file

我正在尝试读取文件并生成二维数组。所以我相信我的构造函数会创建正确的尺寸,我只是不知道如何将实际值输入数组。

文件格式:

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)。

第二行是"North to South(NS)"面值

第三行是"East to West(EW)"面值

第四行是"South to North(SN)"面值

第五行是"West to East(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];



}

我的想法是创建一个与文件中第一行大小相同的二维数组。然后对于外部值,创建四个数组来表示外部。不过,我不知道如何将这些外部数组放入我的二维数组中。

与所有文件读取一样,尝试将每个任务分开。问问自己 "What do I need to know before I do , and what do I have to do in order to complete ?" 希望任务按顺序列出(每个任务只需要文件中其上方的信息),这就是您的问题。

您的任务似乎涉及三个子任务:

  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 逻辑),或者如果它是一个无法解决的问题,则抛出某种异常。