从文本文件创建矩阵 - Java

时间:2017-11-07 02:05:55

标签: java matrix multidimensional-array

我有这个文本文件:

2
2
12
13
23
24
49
59
69
79

前两个数字应该是矩阵的行和列,在这种情况下是2x2。我试图解决的问题是找到一种方法来包含第二个包含第二个矩阵的二维数组。

我的代码:

        Scanner fileInput = new Scanner(new File("input1.txt"));
        int n1 = fileInput.nextInt();
        int n2 = fileInput.nextInt();
        System.out.print("matrix is " + n1 + "x" + n2 +"\n");

        int [][] firstMatrix = new int [n1][n2];
        int [][] secondMatrix = new int [n1][n2];

        for(int i = 0; i < n1; ++i)
        {
            for(int j = 0; j < n2; ++j)
            {
                if(fileInput.hasNextInt())
                {
                    firstMatrix[i][j] = fileInput.nextInt();   
                }                 
            }
        }

        System.out.println("Matrices: ");
        for(int i = 0; i < n1; i++)
        {
            for(int j = 0; j < n2; j++)
            {
                System.out.println(firstMatrix[i][j]);
            }
        }

它只打印以下内容:

12
13
23
24

如何让它从文件中读取接下来的四行整数?了解如何让它看起来像这样:

12 13

23 24
编辑:这种方法似乎有助于最后一个问题:

for(int i=0; i<n1; i++)
        {
            for(int j=0; j<n2; j++)
            {
                System.out.print(firstMatrix[i][j] + " " );
                //System.out.print(secondMatrix[i][j] + " ");

            }
             System.out.println();
        }   

我现在面临的唯一问题是能够包含其他四个整数并将它们变成矩阵。

3 个答案:

答案 0 :(得分:1)

您可以将它们分组为1个数组:

int matrixNumb = 2; // number of matrix
int [][][] matrix = new int [matrixNumb][n1][n2];
for (int h = 0; h < matrixNumb; h++)
{
    for(int i = 0; i < n1; ++i)
    {
        for(int j = 0; j < n2; ++j)
        {
            if(fileInput.hasNextInt())
            {
                matrix [h][i][j] = fileInput.nextInt();   // read from file
            }                 
        }
    }
}

System.out.println("Matrices: ");
for (int h = 0; h < matrixNumb; h++)
{
    for(int i = 0; i < n1; i++)
    {
        for(int j = 0; j < n2; j++)
        {
            System.out.print(matrix[h][i][j]);
            System.out.print("\t"); //How do you want to separate columns?
        }
        System.out.print("\r\n"); //How do you want to separate rows?
    }
    System.out.println(); //How do u want to print next matrix?
}

我没有编译或运行它,但希望它有所帮助。

答案 1 :(得分:0)

您可以按顺序从文件中读取两个数组的数据,如下所示:

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

public class App {

    private static void readMatrix(final Scanner scanner, final int[][] matrix) {
        for (int i = 0; i < matrix.length; ++i) {
            for (int j = 0; j < matrix[i].length; ++j) {
                if (scanner.hasNextInt()) {
                    matrix[i][j] = scanner.nextInt();
                }
            }
        }
    }

    private static void displayMatrix(final int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(final String... args) throws FileNotFoundException {
        final Scanner scanner = new Scanner(new File("input.txt"));

        final int n1 = scanner.nextInt();
        final int n2 = scanner.nextInt();

        System.out.print(String.format("The matrix is %d x %d \n", n1, n2));

        final int[][] firstMatrix = new int[n1][n2];
        final int[][] secondMatrix = new int[n1][n2];

        System.out.println("Reading data to first matrix");
        readMatrix(scanner, firstMatrix);
        System.out.println("Reading data to second matrix");
        readMatrix(scanner, secondMatrix);

        System.out.println("First Matrix");
        displayMatrix(firstMatrix);
        System.out.println("Second Matrix");
        displayMatrix(secondMatrix);
    }
}

答案 2 :(得分:-1)

 for(int i = 0; i < n1*n1; i++)
    {
        for(int j = 0; j < n2*n2; j++)
        {
            if(fileInput.hasNextInt())
            {
               if(i < n1){
                firstMatrix[i][j] = fileInput.nextInt(); 
                } 
               else 
                  {
                   secondMatrix[i][j] = fileInput.nextInt();
                  }
            }                 
        }
    }

你有两个大小为n * n的矩阵,所以你可以这样做: