用Java实现方阵乘法

用Java实现方阵乘法,java,arrays,matrix-multiplication,Java,Arrays,Matrix Multiplication,我正在尝试使用多维数组编写一个简单的平方矩阵乘法方法 package matrixmultiplication; import java.util.Scanner; public class Matrixmultiplication { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int [][] a,b,c;

我正在尝试使用多维数组编写一个简单的平方矩阵乘法方法

package matrixmultiplication;

import java.util.Scanner;


public class Matrixmultiplication 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        int [][] a,b,c;
        int size;
        System.out.print("Enter the Size of the matrix :");
        size = scan.nextInt();
        a=b=c=new int[size][size];
        System.out.println("Enter the elements of the First matrix");
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            {
                System.out.print("Enter the element a[" + i +"]["+ j + "] : ");
                a[i][j] = scan.nextInt();
            }
        }
        System.out.println("Enter the elements of the Second matrix");
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            {
                System.out.print("Enter the element b[" + i +"]["+ j + "] : ");
                b[i][j] = scan.nextInt();
            }
        } 

        System.out.println("The Product of the two matrix is : ");
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            {
                int sum = 0;
                for(int k=0;k<size;k++)
                {
                    sum +=(a[i][k] * b[k][j]);
                }
                c[i][j] = sum;
                System.out.print(c[i][j] + "\t");
            }
            System.out.println();
        }         

    }

}
该程序的正确输出应为:

2  2
2  2
有人能告诉我这个代码有什么问题吗。

问题在这里:

a=b=c=new int[size][size];
这只会创建一个数组,并使所有变量都指向它。因此,更新
c
的元素也就是更新
a
b
的元素

改为创建3个阵列:

a=new int[size][size];
b=new int[size][size];
c=new int[size][size];

为什么不选择一个现有的库,比如?
a=new int[size][size];
b=new int[size][size];
c=new int[size][size];