Java 试图从文件创建矩阵数组,但程序返回两个相同的矩阵?
好的,这个程序的目标是将两个由文件填充的矩阵相加。我的程序可以很好地创建和打印第一个数组,但是当我尝试填充和打印第二个数组时,它只会填充并重新打印第一个数组。我可以解释为什么,但我不知道如何着手解决它 这是文件Java 试图从文件创建矩阵数组,但程序返回两个相同的矩阵?,java,arrays,matrix,netbeans,Java,Arrays,Matrix,Netbeans,好的,这个程序的目标是将两个由文件填充的矩阵相加。我的程序可以很好地创建和打印第一个数组,但是当我尝试填充和打印第二个数组时,它只会填充并重新打印第一个数组。我可以解释为什么,但我不知道如何着手解决它 这是文件 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
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();
}
}
}
该代码可以很好地填充和打印第一个矩阵,但无法在第二个矩阵上执行相同的操作。以下是输出:
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
它只是再次打印第一个矩阵。如何使用同一文件创建两个单独的矩阵 您必须更改读取方法以读取并返回两个矩阵 现在它一直在打开文件,读取第一个条目只是为了返回它。它只显示第一个矩阵,因为当前代码只是停止而不是读取第二个矩阵的数据 您还可以增强文件格式以包含矩阵的数量。然后你必须返回一个矩阵数组,而不是一个,只需读取输入文件中给出的所有信息
但老实说,您的代码和现在一样非常好,我宁愿使用多个文件,并且在每个文件中只保留一个矩阵 您需要确保从正确的行号读取每个矩阵。因此,如果保留行/列编号的记录,则将设置:
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();
}
}
}
而输出将是:
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矩阵之前会有一行,指定即将到来的矩阵的行和列(即
22
)。您可以实现一个helper方法,该方法将扫描下一行并更新行和列,以便正确处理下两个矩阵。
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