java - 如何将 txt 文件读入二维数组 Java

标签 java arrays multidimensional-array try-catch filereader

快速提问,我如何将 txt 文件读入二维数组?我需要将每一行放入一个数组中,以便稍后使用它来测量长度和宽度的总和。这将用于生活游戏。

代码

public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int matrix[][] = new int[size][size];
            while (s.hasNext()) {
                for (int i = 0; i < matrix.length; i++) {
                       for (int col = 0; col < matrix.length; col++) {
                      //      matrix[i][col] = ;
                        }
                }
            }
            s.close();

        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }

文本文档

20 20
    0    2
    0    6
    0    7
    0   10
    0   11
    0   16
    1    0
    1    4
    1    7
    2    6
    2   12
    2   15
    2   19
    3    0
    3    4
    3   14
    3   16
    3   17
    4    0
    4    8
    4   12
    4   14
    4   15
    5    0
    5    2
    5    4
    5    7
    5   13
    5   17
    6   11
    6   14
    6   15
    6   18
    7    4
    7   10
    7   13
    7   14
    8    6
    8   11
    8   12
    8   14
    8   17
    8   19
    9    6
    9    8
    9   13
    9   14
   10    3
   10   10
   10   19
   11    6
   11    7
   11    9
   11   10
   11   17
   12    0
   12    2
   12   12
   13    0
   13    1
   13    7
   13    8
   13   14
   13   15
   14    7
   14    9
   14   12
   14   13
   14   16
   14   18
   15    0
   15    2
   15    6
   15    8
   15   10
   15   11
   15   16
   15   17
   16    2
   16    3
   16   18
   17    2
   17    7
   17   11
   17   13
   17   19
   18    0
   18    5
   18    6
   18   11
   18   12
   18   13
   18   14
   19    3
   19    4
   19    6
   19    8
   19   11
   19   16
   19   17

最佳答案

因此,文本文件似乎包含生命游戏中生命在哪里的所有对。

您所做的是增加每次读取的“i”值,但这不是保证。

相反,以下协议(protocol)似乎是文本文件之一。

  1. 行包含棋盘的 X 和 Y 尺寸。

其余的线条包含有生命的地方。

所以你的代码应该看起来更像这样(除了你已经提供的之外不包括异常处理)

 public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int rows = s.nextInt();
            int columns = s.nextInt();
            int matrix[][] = new int[rows][columns];
            while (s.hasNextInt()) {
                int row = s.nextInt();
                int col = s.nextInt(); // If this fails the text documnet is incorrect.
                matrix[row][col] = 1;
            }
            s.close();
            // At this point all dead cells are 0 and all live are 1
        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }

关于java - 如何将 txt 文件读入二维数组 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35147544/

相关文章:

java - 如何在LWJGL 3中使用WGLARB?

ios - 如何使用 Swift 对数组数组(按变量名称)进行排序?

javascript - 将新属性推送到数组中的对象

javascript - Chart.JS 多线图表,来自 JSON 的行数未知

python - 更改不在索引列表中的 NumPy 数组的值

java - SQLiteOpenHelper 和关闭方法

java - 帮助在 Eclipse 中创建重构插件?

ISO-8859-9 的 java xml 解析

c - 为什么数组的地址等于它在 C 中的值?

c++ - 如何将二维数组值与用户输入进行比较?