【问题标题】:Reading files that contains different dimensions for a 2D array读取包含二维数组不同维度的文件
【发布时间】:2017-03-13 00:56:17
【问题描述】:

我正在尝试读取文本文件。文件是这样的:

第一行是网格数

第二行是m x n矩阵

然后我们有单元格的元素,然后它进入另一行的另一个 m x n 矩阵,然后是它的元素,依此类推。

示例图片将进一步阐明我的描述。

现在我知道如何使用Scanner 和读取文件,但问题是我正在硬编码矩阵维度,因为我想要一个可以处理任何文件的算法。文件将保持不变,只是尺寸会改变。

现在我的算法是这样的:

 Get the name of the file as command line argument
 then read the first line which is the number of grids to create
 then read the next line which states the dimension of the first grid
 then create a 2D array of that dimension. 
 Then read only lines up to the rows specified and load the data
 Then read the dimensions for the next grid and loop on till the end.

现在很容易获得算法,但我不知道如何将其转换为可行的代码。我可以使用Scanner,我也知道BufferedReader,但我更喜欢Scanner,因为它包含更多功能。我的问题是我应该如何处理具有不同矩阵尺寸和网格数量的文件。

One example of a test file to read

【问题讨论】:

  • “现在很容易获得算法,但我不知道如何将其转换为可行的代码” -- 为什么不呢?您希望我们为您编写代码吗?抱歉,但这不是本网站的运作方式。 “我想做 X,请给我提示和/或示例代码” 形式的问题被视为离题。请访问help center并阅读How to Ask,尤其是阅读Why is “Can someone help me?” not an actual question?
  • 我没有询问提示或示例代码,我只是询问我应该如何处理不同的文件,这是一种适用于所有文件而不仅仅是一个文件的算法。我拥有的算法是针对问题底部的示例文件进行硬编码的。我已经有一个适用于一个文件的代码,但我将读取不同的文件,并且我拥有的算法不适用于它们。那么我可以对该算法进行哪些调整,这是我的问题@JimGarrison

标签: java file matrix java.util.scanner bufferedreader


【解决方案1】:

首先,需要分离输入文件中的矩阵元素(即矩阵中的数字)。示例图像中的矩阵元素不是,这可能会导致问题(例如,使用您当前的输入文件格式方法,无法区分矩阵 [1 11] 和 [11 1])。

您可以使用下面的 getArrayOfMatrices 方法来实现您的算法;此方法将输入文件名作为参数并返回矩阵的二维数组表示形式的数组。您可以访问(其中 a 表示方法返回的数组):
(1) 第一个矩阵的二维数组a[0]
(2) 第二个矩阵的二维数组a[1]
(3) 第一个矩阵的 (1, 1)-entry 与a[0][0][0]
(4) 第一个矩阵的 (1, 2)-entry 与a[0][0][1]
(5) 第一个矩阵的 (2, 1) 项与a[0][1][0]
(6) 第一个矩阵的 (2, 2) 项与a[0][1][1]
(7) 第二个矩阵的 (1, 1) 项与a[1][0][0]
(8) 第二个矩阵的 (1, 2)-entry 与a[1][0][1]
(9) 第二个矩阵的 (2, 1) 项与a[1][1][0]
(10) 第二个矩阵的 (2, 2) 项与a[1][1][1]
等等。

对于此代码采用的方法,假设输入文件中的矩阵元素由空格分隔。

以下是可与 getArrayOfMatrices 方法一起使用的输入文件内容示例:
2
1 1
2
2 3
-1 0 1
3 30 300

方法代码如下:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ObtainmentOfArrayOfMatrices {

    public static int[][][] getArrayOfMatrices(String inputFileName)
    throws FileNotFoundException {
        Scanner scanner = new Scanner(new File(inputFileName));
        int numberOfMatrices = Integer.parseInt(scanner.nextLine());

        // An array that will contain all the 2D array representations of the
        // matrices.
        int[][][] arrayOfMatrices = new int[numberOfMatrices][][];

        int numberOfMatricesGoneOver = 0;
        while(numberOfMatricesGoneOver < numberOfMatrices) {

            // Create a 2D array representation of the current matrix and put
            // that representation in an array.

            String[] dimensions = scanner.nextLine().split(" ");
            int numberOfRows = Integer.parseInt(dimensions[0]);
            int numberOfColumns = Integer.parseInt(dimensions[1]);

            // A 2D array which represents the current matrix.
            int[][] currentMatrix = new int[numberOfRows][numberOfColumns];

            int numberOfRowsGoneOver = 0;
            while(numberOfRowsGoneOver < numberOfRows) {

                // An array that represents the current matrix row.
                int[] currentRow = new int[numberOfColumns];

                String[] elementsInCurrentRow = scanner.nextLine().split(" ");
                int numberOfColumnsGoneOverInCurrentRow = 0;

                // Insert the elements that are in the current matrix row.
                while(numberOfColumnsGoneOverInCurrentRow < numberOfColumns) {
                    currentRow[numberOfColumnsGoneOverInCurrentRow] = 
                    Integer.parseInt(elementsInCurrentRow
                    [numberOfColumnsGoneOverInCurrentRow]);
                    numberOfColumnsGoneOverInCurrentRow++;
                }

                // Insert the complete array representation of the current
                // matrix row.
                currentMatrix[numberOfRowsGoneOver] = currentRow;

                numberOfRowsGoneOver++;
            }

            // At this point, there's a complete 2D array representation of
            // the current matrix.

            arrayOfMatrices[numberOfMatricesGoneOver] = currentMatrix;
            numberOfMatricesGoneOver++;
        }
        scanner.close();
        return arrayOfMatrices;
    }
}

【讨论】:

    【解决方案2】:

    首先在程序的开头声明一个矩阵:

    int[][] matrix;
    int n,m;
    for each matrix on the file:
        read matrix dimensions to n and m
        matrix = new int[n][m]
        read values and put them on matrix
    

    其他替代方法可以是声明Vector&lt;Vector&lt;Integer&gt;&gt;,如here 所述,或具有表示矩阵的类,如this one

    【讨论】:

    • 这与我的算法有何不同?你基本上重复了我在算法中的内容。
    • 那我不明白你到底想要什么。您说问题在于您在代码中对矩阵维度进行了硬编码。使用我向您介绍的解决方案,您无需对尺寸进行硬编码。如果有的话,您只是对矩阵上元素的类型进行硬编码。
    猜你喜欢
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多