简体   繁体   English

Java二维数组乘法错误?

[英]Java Two-Dimensional Array Multiplication Error?

I am having to write this program for school that multiplies a pair of two-dimensional arrays. 我必须为学校编写该程序,该程序将一对二维数组相乘。 I keep getting an out of bounds:3 error when I try to run the program. 当我尝试运行程序时,我不断出现出界错误:3错误。

I have gone over the code many times and can't for the life of me figure out where the breakdown is happening. 我已经遍历了很多遍代码,无法终生确定发生故障的位置。 I ran a debug and this first breakpoint was at the if statement "if (a[0].length != b.length)" I couldn't figure out why that was a breakpoint. 我运行了一个调试程序,第一个断点在if语句“ if(a [0] .length!= b.length)”处,我不知道为什么这是断点。

Could someone help me? 有人可以帮我吗?

    public class Multiply {

    public static double[][] Multiply_2D_Arrays(double[][] a, double[][] b)

    { 
        if (a[0].length != b.length) // Check to see if the number of a's colums equals the number of b's rows
        {
            throw new IllegalArgumentException("Matrices don't match: " + a[0].length + " != " + b.length);

        }

        int a_rows = a.length;      // Defines the variable M as the row length of array a
        int b_columns = b[0].length;   // Defines the variable N as the column length of array b
        double[][] c = new double[a_rows][b_columns]; // This means that the dimensions of array c will be the rows of a by the columns of b

        for(int i = 0; i < a.length; i++)
            {
                for(int j = 0; j < b[0].length; j++)
                {
                    for(int k = 0; k < a[0].length; )
                    {

                        c[i][j] += a[i][k] * b[k][j];     //Iterates through each row and column of array a and b and then adding it to the dot point sum

                    }

                }
            }

            return c;      //returns the final new array c

        }

    public static void main(String[] args)

    {
        double[][] array1 = {{4.0, 5.0, 6.0}, {2.0, 1.0, 4.0}, {8.0, 7.0, 6.0}, {1.0, 1.0, 2.0}};
        double[][] array2 = {{5.0, 7.0, 7.0, 8.0}, {8.0, 8.0, 9.0, 2.0}, {10.0, 2.0, 3.0, 1.0}};
        double[][] array3 = Multiply.Multiply_2D_Arrays(array1, array2); //Calls the Multiply_2D_Arrays method

        for(int i = 0; i < array3.length; i++)
        {
            for (int j = 0; j < array3.length; j++)
            {
                System.out.print(array3[i][j] + " ");
            }
        }

        System.out.println();
    }

}

You have missed k++ in for(int k = 0; k < a[0].length; ) 您错过了for(int k = 0; k < a[0].length; ) k++

Then you have an infinity loop 然后你有一个无限循环

Also if you want to print the result as matrix, put System.out.println() inside for loop : 另外,如果要将结果打印为矩阵,请将System.out.println()放入for循环中:

    for(int i = 0; i < array3.length; i++)
    {
        for (int j = 0; j < array3.length; j++)
        {
            System.out.print(array3[i][j] + " ");
        }
        System.out.println();
    }

如果将for(int k = 0; k < a[0].length; )更改for(int k = 0; k < a[0].length;k++ ) ,则程序将正确执行,而您错过了其中的k++ for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM