【问题标题】:How to create a 2D array by scanning a file如何通过扫描文件创建二维数组
【发布时间】:2015-04-06 17:51:15
【问题描述】:

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

文件格式:

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];



}

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

【问题讨论】:

  • 单独跟踪外部数组比它的工作更多。我可能会将它们全部放在一个数组中,只是使二维数组 2 在两个方向上都更大。

标签: java multidimensional-array java.util.scanner


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

【讨论】:

  • 感谢您的详细回答。只是一个快速的问题。如何打印出我的二维数组?
  • 当我尝试打印配置时,我得到了SkyscraperConfig@4fca772d
  • 检查 Arrays.deepToString(...)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-08
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多