【问题标题】:Attempting to create a matrix array from a file, but program returns two of the same matrix?尝试从文件创建矩阵数组,但程序返回两个相同的矩阵?
【发布时间】:2017-06-07 11:03:37
【问题描述】:

好的,程序的目标是将两个由文件填充的矩阵相加。我的程序可以很好地创建和打印第一个数组,但是当我尝试填充和打印第二个数组时,它只会填充并重新打印第一个数组。我可以证明原因,但我不确定如何解决它。

这是文件

3 4

2 1 7 -10
0 5 -3 12
1 7 -2 -5

0 1 2 3
4 5 6 7
8 9 0 1

3 和 4 只是用于确定行和列,如下面的代码所示。所以第一个数组/矩阵是:

 2 1 7 -10
 0 5 -3 12
 1 7 -2 -5

第二个

0 1 2 3
4 5 6 7
8 9 0 1

这是我的代码:

public class Matrices {

    public static int[][] readMatrix(int rows, int columns, String file) throws FileNotFoundException {
        Scanner fileReader = new Scanner(new FileInputStream(file));
        rows = fileReader.nextInt();
        columns = fileReader.nextInt();
        int[][] result = new int[rows][columns];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                result[i][j] = fileReader.nextInt();

            }
        }
        return result;
    }

    public static void printMatrix(int[][] matrix) {
        int rows = matrix.length;
        int columns = matrix[0].length;

        for (int i = 0; i < rows; i++) {
           for (int j = 0; j < columns; j++) {
               System.out.print(matrix[i][j] + " ");
            }
           System.out.println();
        }
    }
}

还有我的司机:

public class MatricesDriver {
    public static void main(String[] args)throws FileNotFoundException {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter name of file: ");
        String filename = keyboard.next();
        File file = new File(filename);
        int rows = 0;
        int columns = 0;

        int[][] a = Matrices.readMatrix(rows, columns, filename);
        int[][] b = Matrices.readMatrix(rows, columns, filename);

        Matrices.printMatrix(a);
        System.out.println();
        Matrices.printMatrix(b);
    }
}

代码可以很好地填充和打印第一个矩阵,但不能在第二个矩阵上执行相同的操作。这是输出:

Enter name of file: 
data/MAtrices.txt
2 1 7 -10 
0 5 -3 12 
1 7 -2 -5 

2 1 7 -10 
0 5 -3 12 
1 7 -2 -5

它只是再次打印第一个矩阵。如何使用同一个文件创建两个单独的矩阵?

【问题讨论】:

    标签: java arrays matrix netbeans


    【解决方案1】:

    你必须改变你的读取方法来读取并返回两个矩阵!

    现在它一直在打开文件,读取第一个条目只是为了返回它。它仅显示第一个矩阵,因为您当前的代码只是停止而不是读取第二个矩阵的数据。

    您也可以增强文件格式以包含矩阵的数量。然后你必须返回一个矩阵数组而不是一个矩阵,只需读取输入文件中给出的所有信息。

    但老实说,你的代码和现在一样好,我宁愿使用多个文件并在每个文件中只保留一个矩阵。

    【讨论】:

      【解决方案2】:

      您需要确保从正确的行号读取每个矩阵。因此,如果您保留行/列号的记录,那么您将被设置:

      class Matrices {
      private Scanner fileReader;
      private int rows;
      private int columns;
      
      public Matrices(String file) throws FileNotFoundException {
          this.fileReader = new Scanner(new FileInputStream(file));
          rows = fileReader.nextInt();
          columns = fileReader.nextInt();
      }
      
      public int[][] readMatrix() throws FileNotFoundException {
      
          int[][] result = new int[rows][columns];
      
          for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                  result[i][j] = fileReader.nextInt();
      
              }
          }
          return result;
      }
      
      public static void printMatrix(int[][] matrix) {
          for ( int[] anArray : matrix ) {
              for ( int anInt : anArray ) {
                  System.out.print(anInt+ " ");
              }
              System.out.println();
          }
      }
      }
      

      main 方法将变为:

      public static void main(String[] args) throws FileNotFoundException {
          Scanner keyboard = new Scanner(System.in);
          System.out.println("Enter name of file: ");
          String filename = keyboard.next();
      
          Matrices matrixReader = new Matrices(filename);
          int[][] a = matrixReader.readMatrix();
          int[][] b = matrixReader.readMatrix();
      
          Matrices.printMatrix(a);
          System.out.println();
          Matrices.printMatrix(b);
      }
      

      输出将是:

      2 1 7 -10 
      0 5 -3 12 
      1 7 -2 -5 
      
      0 1 2 3    
      4 5 6 7 
      8 9 0 1 
      

      【讨论】:

      • 好吧,这似乎行得通。但我还有另一个问题。我两个可以用这种方法创建两个不同大小的二维数组吗?例如,上面的一个是添加两个 3x4 矩阵。如果我想立即从同一个文件中获取两个 2x2 数组之间的差异怎么办?这可能吗?
      • 您可以这样做,因为在两个 2x2 矩阵之前会有一行,指定即将到来的矩阵的行和列(即2 2)。您可以实现一个辅助方法,该方法将扫描下一行并更新行和列,以便正确处理接下来的几个矩阵。
      猜你喜欢
      • 2013-09-15
      • 2021-05-25
      • 2015-10-17
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多