矩阵乘法返回数组索引超出范围的错误

时间:2015-03-30 05:02:03

标签: java arrays matrix indexoutofboundsexception filereader

这是这里的第一篇文章,如果我的问题在这里没有达到要求的标准,请原谅我。

我编写了一段代码,它从两个独立的文件中获取两个矩阵的输入,并执行乘法并将数据输出到一个新文件。

它为2x3或3x3矩阵提供了完美的输出。如果我输入4x4矩阵,我得到数组索引超出绑定运行时异常。我动态创建索引

时,我不明白这个原因

我在第40行得到一个超出绑定异常的数组索引。

我收到错误。

![Snipet] [2]

  1. 列出项目

    public class MM {
    
    
    
    
    private BufferedReader br;
    private int sum = 0;
    private final static String matrixA="matrixA.txt"; 
    private final static String matrixB="matrixB.txt";
    
    public static void main(String[] args) {
        new MM().MathMultiplicationValues(matrixA, matrixB);
    }
    
    private void MathMultiplicationValues(String mat1, String mat2) {
        try {
            br = new BufferedReader(new FileReader(mat1));          
    
            String line;
            int mat1rows = 0, mat1cols = 0, mat2rows = 0, mat2cols = 0;
            while ((line = br.readLine()) != null) {
                mat1cols = line.split(" ").length + 1;
                mat1rows++;
            }
            br.close(); // To close file
            br = new BufferedReader(new FileReader(mat2)); // to read input from file.
            while ((line = br.readLine()) != null) {
                mat2cols = line.split(" ").length + 1;
                mat2rows++;
            }
            int[][] mat1vals = new int[mat1rows ][mat1cols ]; 
        int[][] mat2vals = new int[mat2rows ][mat2cols ]; 
            br.close();
            br = new BufferedReader(new FileReader(mat1));
            for (int i = 1; i < mat1rows + 1; i++) {
                line = br.readLine();
                String[] colvals = line.split(" ");
                for (int j = 1; j < mat1cols; j++) {
                    mat1vals[i][j] = Integer.parseInt(colvals[j - 1]);
    
                }
    
            }
    
    
            br.close();
            br = new BufferedReader(new FileReader(mat2));
            for (int i = 1; i < mat2rows + 1; i++) {
                line = br.readLine();
                String[] colvals = line.split(" ");
                for (int j = 1; j < mat2cols; j++) {
                    mat2vals[i][j] = Integer.parseInt(colvals[j - 1]);
    
                }
    
    
            }
    
            br.close();
            if ((mat1cols-1) == mat2rows) { 
                int[][] resltmat = new int[mat1rows + 1][mat2cols + 1];
                for (int i = 1; i < mat1rows + 1; i++) { //Loop does matrix multiplication. 
                    for (int j = 1; j < mat1cols; j++) {
                        for (int k = 0; k < mat2rows + 1; k++)
                            sum = sum + mat1vals[i][k] * mat2vals[k][j];
                        resltmat[i][j] = sum;
                        sum = 0;
                    }
                }
    
                final PrintWriter pw = new PrintWriter("Answer.txt"); //Creates a new file called Matrix Answer. 
                for (int i = 1; i < mat1rows + 1; i++) 
                {
    
                    for (int j = 1; j < mat2cols; j++) {
                        pw.print(resltmat[i][j] + " "); // Writes the output to file the file called MatrixAnswer
    
                    }
                    pw.println();
    
                }
                pw.close();
            } else // If no of columns not equal to rows control passes to else block. 
                System.out.println("Multiplication of Matrix is not possible because columns are not equal to rows");
    
        } catch (Exception e) {
            e.printStackTrace();
    
        }
    }
    
    }
    

2 个答案:

答案 0 :(得分:1)

可能是因为这个

for (int i = 1; i < mat1rows + 1; i++) {
    line = br.readLine();
    String[] colvals = line.split(" ");
    for (int j = 1; j < mat1cols; j++) {
        mat1vals[i][j] = Integer.parseInt(colvals[j - 1]);

    }

}

i = mat1rows在最后一次迭代,即OOB。将for (int i = 1; i < mat1rows + 1; i++)更改为for (int i = 1; i < mat1rows; i++)

答案 1 :(得分:0)

正如您在分配中使用的那样,结果矩阵的维度为mat1rows x mat2cols。因此,在resltmat[i][j]的计算中,索引i已绑定mat1rows(检查),而索引j具有上限mat2cols(失败)。因此,将j的范围从mat1cols更改为mat2cols