Java矩阵点积ArrayIndexOutOfBoundsException

Java矩阵点积ArrayIndexOutOfBoundsException,java,matrix-multiplication,Java,Matrix Multiplication,我在第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

我在第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);
        }     
    }
}
包装阵列产品;
导入javax.swing.JOptionPane;
公共类ArrayProducts{
公共静态void main(字符串[]args){
字符串输出=”;
int rowA=Integer.parseInt(JOptionPane.showInputDialog(“\n输入矩阵的行数”);
int colA=Integer.parseInt(JOptionPane.showInputDialog(“\n输入矩阵的列数”);
int rowB=Integer.parseInt(JOptionPane.showInputDialog(“\n输入MatrixB的行数”);
int colB=Integer.parseInt(JOptionPane.showInputDialog(“\n输入MatrixB的列数”);
如果(可乐!=rowB){
输出+=“无法执行矩阵运算:内部矩阵维度必须一致。”;
输出+=“\n矩阵的维度为”+rowA+“x”+colA+”;
输出+=“\n矩阵XB的维度为”+rowB+“x”+colB+”;
showMessageDialog(null,输出);
回来
}否则{
输出+=“\n结束产品开始:”;
int[]a=新int[rowA][colA];
输出+=“\n矩阵的维度为”+rowA+“x”+colA+”;
int[]b=新int[rowB][colB];
输出+=“\n矩阵的维度为”+rowB+“x”+colB+”;
showMessageDialog(null,输出);
int[][]c=新int[rowA][colB];
////
//输入第一个矩阵
for(int i=0;i
我猜您是想在下面的代码中使用索引I、j、k而不是rowA、colB等

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

我将向您展示一个简单的示例

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
循环中,我想你应该使用
I
j
k