【问题标题】:Java Matrices Dot Product ArrayIndexOutOfBoundsExceptionJava 矩阵点积 ArrayIndexOutOfBoundsException
【发布时间】:2018-06-26 13:57:39
【问题描述】:

我在第 66 行 c[rowA][colB] = c[rowA][colB] + a[rowA][colA]*b[colA][colB]; 上遇到错误。我手动浏览了索引,只是无法弄清楚索引出错的地方。非常感谢您的帮助。

package arrayproducts;
import javax.swing.JOptionPane;
public class ArrayProducts {
    public static void main(String[] args) {
        String output = "";
        int rowA = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of rows for MatrixA."));
        int colA = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of columns for MatrixA."));
        int rowB = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of rows for MatrixB."));
        int colB = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of columns for MatrixB."));
        if( colA != rowB){
            output += "Cannot perform matrix operation: Inner matrix dimensions must agree.";
            output += "\nMatrixA has a dimension of "+ rowA + " x " + colA + ".";
            output += "\nMatrixB has a dimension of "+ rowB + " x " + colB + ".";
            JOptionPane.showMessageDialog(null, output);
            return;

        } else {
            output += "\nDot Product Begin:";
            int [][] a = new int[rowA][colA];
            output += "\nMatrixA has a dimension of "+ rowA + " x " + colA + ".";
            int [][] b = new int[rowB][colB];
            output += "\nMatrixA has a dimension of "+ rowB + " x " + colB + ".";
            JOptionPane.showMessageDialog(null, output);
            int [][] c = new int[rowA][colB];
            ////
            // enter first matrix
            for(int i = 0; i < rowA; i++){
                for(int j = 0; j < colA; j++){
                    a[i][j] = Integer.parseInt(
                        JOptionPane.showInputDialog("\nEnter an integer for MatrixA, row " + (i+1) + " and column " + (j+1) + "."));
                }
            }
            // add first matrix to output
            output += "\nThe first matrix is: \n";
            for(int i=0; i < rowA; i++){
                for(int j=0; j < colA; j++){
                    output += "    " + a[i][j];
                }
                output += "\n";
            }
            JOptionPane.showMessageDialog(null, output);

            ////
            // enter second matrix
            for(int i = 0; i < rowB; i++){
                for(int j = 0; j < colB; j++){
                    b[i][j] = Integer.parseInt(
                        JOptionPane.showInputDialog("\nEnter an integer for MatrixB, row " + (i+1) + " and column " + (j+1) + "."));
                }
            }
            // add second matrix to output
            output += "\nThe second matrix is: \n";
            for(int i=0; i < rowB; i++){
                for(int j=0; j < colB; j++){
                    output += "    " + b[i][j];
                }
                output += "\n";
            }
            JOptionPane.showMessageDialog(null, output);

            ////
            // compute the product
            for(int i = 0; i < rowA; i++){
                for(int j = 0; j < colB; j++){
                    for(int k = 0; k < colA ; k++){ // either colA or rowB will work here
                        c[rowA][colB] = c[rowA][colB] + a[rowA][colA]*b[colA][colB];
                    }
                }
            }
            output += "\nThe product of MatrixA and MatriB is:\n";
            for(int i=0; i < rowA; i++){
                for(int j=0; j < colB; j++){
                    output += "    " + c[rowA][colB];
                }
                output += "\n";
            }
            JOptionPane.showMessageDialog(null, output);
        }     
    }
}

【问题讨论】:

    标签: java matrix-multiplication


    【解决方案1】:

    我猜你的意思是在下面的代码中使用索引 i、j、k 而不是 rowA、colB 等。

    c[rowA][colB] = c[rowA][colB] + a[rowA][colA]*b[colA][colB];
    

    【讨论】:

      【解决方案2】:

      我会给你一个简单的例子。

      int[] a = new int[3];
      

      这意味着a 只能有 3 个值。

      a[0], a[1] and a[2]
      

      如果您尝试a[3],它将超出范围。

      所以。你有

      int [][] c = new int[rowA][colB];
      

      并尝试访问超出范围的c[rowA][colB]

      在您的三个for 循环中,我认为您想使用ijk

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-31
        • 1970-01-01
        • 2014-02-01
        • 2016-08-15
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        相关资源
        最近更新 更多