变量可能尚未在二维数组中初始化

我有一个静态 matrixMult 和 static matrixAdd 方法和一个静态 matrixDisplay(用于打印结果),我在 Main 函数中编写了一个测试示例。


我的目标:我想用这两个静态方法相乘和相加两个矩阵。


我收到这些错误“变量 C 和 d 可能尚未初始化”。有人可以告诉我,问题是什么?


public class Matrixmultadd {


    static double[][] matrixMult(double[][] A,double[][] B) {

        double[][] C; //declar this variable for return the result

        //return null if on of matrix are null

        if(A == null || B == null){

            return null;

        }



        if(A[1].length == B.length){ //check to be equal columns of A with rows of B

            for(int n = 0;n < A.length;n++){//n is numbers of rows of A

                for(int k = 0;k < B[n].length;k++){

                    C[n][k] = 0.0;

                     for(int l = 0;l < A[n].length;l++){//row n of A multiple in column k of B

                        C[n][k] += A[n][l] * B[l][k];


                    }

                }


            }

        return C;

        } else {

        return null;

        }


    }



    static double[][] matrixAdd(double[][] a,double[][] b) {


    //check the rows and columns of a and b are equal

        if(a.length == b.length && a[1].length == b[1].length){

            double[][] d; //declar this variable for return the result

            for(int n = 0;n < b.length;n++){

                for(int m = 0;m < b[1].length;m++){

                    d[n][m] = a[n][m] + b[n][m];

                }

            }

            return d;

        }else {

            return null;

        }


    } 



    static void matrixDisplay(double[][] a){


        for(int i = 0; i < a.length;i++){

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

                System.out.print(a[i][k] + "\t");

            }

        System.out.println();

        }


    }


public static void main(String[] args){

    double[][] A = {{1,2,3},{4,5,6}}; 


    double[][] B= {{1,2},{3,4},{5,6}};


    double[][] d;

    d = matrixMult(A,B);

    matrixDisplay(d);

}


}


噜噜哒
浏览 136回答 5
5回答

慕容708150

您必须初始化这些变量。在您的情况下,它将类似于:对于 C - double[][] C = new double[A.length][B.length];对于 d double[][] d = new double[a.length][b.length];&nbsp; &nbsp; static double[][] matrixMult(double[][] A,double[][] B) {&nbsp; &nbsp; &nbsp; &nbsp; double[][] C = new double[A.length][B.length]; //declar this variable for return the result&nbsp; &nbsp; &nbsp; &nbsp; //return null if on of matrix are null&nbsp; &nbsp; &nbsp; &nbsp; if(A == null || B == null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(A[1].length == B.length){ //check to be equal columns of A with rows of B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int n = 0;n < A.length;n++){//n is numbers of rows of A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int k = 0;k < B[n].length;k++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C[n][k] = 0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int l = 0;l < A[n].length;l++){//row n of A multiple in column k of B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C[n][k] += A[n][l] * B[l][k];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return C;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static double[][] matrixAdd(double[][] a,double[][] b) {&nbsp; &nbsp; double[][] d = new double[a.length][b.length]; //declar this variable for return the result&nbsp; &nbsp; //check the rows and columns of a and b are equal&nbsp; &nbsp; &nbsp; &nbsp; if(a.length == b.length && a[1].length == b[1].length){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int n = 0;n < b.length;n++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int m = 0;m < b[1].length;m++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d[n][m] = a[n][m] + b[n][m];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return d;&nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; static void matrixDisplay(double[][] a){&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < a.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int k = 0;k < a[1].length;k++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(a[i][k] + "\t");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }public static void main(String[] args){&nbsp; &nbsp; double[][] A = {{1,2,3},{4,5,6}};&nbsp;&nbsp; &nbsp; double[][] B= {{1,2},{3,4},{5,6}};&nbsp; &nbsp; double[][] d;&nbsp; &nbsp; d = matrixMult(A,B);&nbsp; &nbsp; matrixDisplay(d);}}

德玛西亚99

您应该在使用它们之前初始化变量 C 和 d,因为 Java 将数组视为对象并且它们被分配在堆内存中。double[][] C = new double[A.length][B[1].length]double[][] d = new double[a.length][b[1].length]

牛魔王的故事

不知道你用Java多久了。在java中,使用数组,你必须1. create a block of memory for it and assign it to a referencedouble[][] matrix; // now, matrix == nullmatrix = new double[10][10]; // now matrix is the address of the memory2. initialize it&nbsp;matrix[0][3]=03. access itSystem.out.println(matrix[0][3])在您的情况下,在上述第一步中声明了 C 和 d ,没有为其分配任何内存块并保留为空,这意味着当您初始化或访问它时,您将触发空指针异常double[][] C; //declar this variable for return the result return null if on of matrix are nulldouble[][] d; //declar this variable for return the result在您的情况下,请添加以下内容以修复它。double[][] C = new double[A.length][B[1].length]double[][] d = new double[b.length][b[1].length]Java 数组的使用,这可能是一个很好的例子 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

HUH函数

您尚未初始化数组变量的维度,因此编译器将不知道需要分配数组的大小。双[][] C=新双[A.length][B[0].length]; 双[][] d=新双[b.length][b[0].length];同样在您的代码中的许多地方,您使用基于 1 的索引来测量 2D 数组中的列数,这可能导致违反内存访问(OutofBound Exception)这是修改后的程序。public class Matrixmultadd {static double[][] matrixMult(double[][] A,double[][] B) {&nbsp; &nbsp; double[][] C=new double[A.length][B[0].length]; //declar this variable for return the result&nbsp; &nbsp; //return null if on of matrix are null&nbsp; &nbsp; if(A == null || B == null){&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; if(A[0].length == B.length){ //check to be equal columns of A with rows of B&nbsp; &nbsp; &nbsp; &nbsp; for(int n = 0;n < A.length;n++){//n is numbers of rows of A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int k = 0;k < B[0].length;k++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C[n][k] = 0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int l = 0;l < A[0].length;l++){//row n of A multiple in column k of B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C[n][k] += A[n][l] * B[l][k];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; return C;&nbsp; &nbsp; } else {&nbsp; &nbsp; return null;&nbsp; &nbsp; }}static double[][] matrixAdd(double[][] a,double[][] b) {//check the rows and columns of a and b are equal&nbsp; &nbsp; if(a.length == b.length && a[0].length == b[0].length){&nbsp; &nbsp; &nbsp; &nbsp; int row=b.length;&nbsp; &nbsp; &nbsp; &nbsp; int col=b[0].length;&nbsp; &nbsp; &nbsp; &nbsp; double[][] d=new double[row][col]; //declar this variable for return the result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for(int n = 0;n <row;n++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int m = 0;m <col;m++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d[n][m] = a[n][m] + b[n][m];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return d;&nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}&nbsp;static void matrixDisplay(double[][] a){&nbsp; &nbsp; int row=a.length;&nbsp; &nbsp; int col=a[0].length;&nbsp; &nbsp; for(int i = 0; i < row;i++){&nbsp; &nbsp; &nbsp; &nbsp; for(int k = 0;k < col;k++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(a[i][k] + "\t");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }}public static void main(String[] args){ double[][] A = {{1,2,3},{4,5,6}};double[][] B= {{1,2},{3,4},{5,6}};double[][] d;d = matrixMult(A,B);matrixDisplay(d);}}

明月笑刀无情

由于您的matrixMult方法中有条件语句,因此有可能永远不会满足条件。所以你的声明&nbsp;double[][]&nbsp;C;可能永远不会被初始化,这将导致错误return&nbsp;C;所以你可以通过初始化它来修复它,例如&nbsp;double[][]&nbsp;C&nbsp;=&nbsp;new&nbsp;double[length][length];&nbsp;//&nbsp;your&nbsp;actual&nbsp;dimensions除此之外,您C在 if 语句中缺少数组的分配语句。您不能将值分配给未初始化的数组。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java