【问题标题】:variable might not have been initialized in 2-d Array变量可能尚未在二维数组中初始化
【发布时间】:2019-04-15 07:57:49
【问题描述】:

我有一个静态 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);
}

}

【问题讨论】:

  • 您在 if 块之后执行 return C,但如果该语句从未计算为 true,则不会被实例化
  • @ScaryWombat 实际上C 从未被初始化(实例化),无论AB 的长度是多少; C[n][k] = ... 还不够 - 需要 C = new double[...][...]

标签: java


【解决方案1】:

您必须初始化这些变量。在你的情况下,它会是这样的:

对于 C - double[][] C = new double[A.length][B.length];

For d double[][] d = new double[a.length][b.length];

    static double[][] matrixMult(double[][] A,double[][] B) {
        double[][] C = new double[A.length][B.length]; //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) {

    double[][] d = new double[a.length][b.length]; //declar this variable for return the result
    //check the rows and columns of a and b are equal
        if(a.length == b.length && a[1].length == b[1].length){
            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);
}

}

【讨论】:

  • 非常感谢。这段代码对我有用。我必须写“new double[a.length][b.length]”。只是我写的第一个 double[][] C =新双[A.length][B[1].length];
  • 酷。如果此答案出于某种原因适合您的 Q. 认为它已获批准(✓ 低于答案的投票计数 1)@Navidms
【解决方案2】:

您应该在使用之前初始化变量 C 和 d,因为 Java 将数组视为对象,并且它们被分配在堆内存中。

double[][] C = new double[A.length][B[1].length]

double[][] d = new double[a.length][b[1].length]

【讨论】:

    【解决方案3】:

    由于您的matrixMult 方法中有条件语句,因此有可能永远不会满足条件。所以你的声明

     double[][] C;
    

    可能永远不会被初始化,这将导致错误

    return C;
    

    所以你可以通过初始化它来修复它,例如

     double[][] C = new double[length][length]; // your actual dimensions
    

    除此之外,您的 if 语句中缺少 C 数组的分配语句。您不能为未初始化的数组赋值。

    【讨论】:

    • 实际上错误必须(也)发生在`C[n][k] = 0.0;`之前,条件无关紧要,根本没有C的初始化。
    • @CarlosHeuberger 是的,这是我在回答中的最后一句话。
    • 但是关于条件句的部分是错误的(准确地说“可以修复它”也是错误的;别无选择,你必须使用new double[.][.])跨度>
    • @CarlosHeuberger 我说过了吗?
    • 第一句话:“由于您的 matrixMult 方法中有条件语句,因此这里有可能条件永远不会满足......”和后来的“...... 可能永远不会被初始化..."
    【解决方案4】:

    您还没有初始化数组变量的维度,因此编译器将不知道需要分配数组的大小。

    double[][] C=new double[A.length][B[0].length]; 双[][] d=新双[b.length][b[0].length];

    在您的代码中,您在许多地方也使用了基于 1 的索引来测量二维数组中的列数,这可能导致违反内存访问(OutofBound Exception)

    这是修改后的程序。

    public class Matrixmultadd {
    
    static double[][] matrixMult(double[][] A,double[][] B) {
        double[][] C=new double[A.length][B[0].length]; //declar this variable for return the result
        //return null if on of matrix are null
        if(A == null || B == null){
            return null;
        }
    
    
        if(A[0].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[0].length;k++){
                    C[n][k] = 0.0;
                     for(int l = 0;l < A[0].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[0].length == b[0].length){
            int row=b.length;
            int col=b[0].length;
            double[][] d=new double[row][col]; //declar this variable for return the result            
            for(int n = 0;n <row;n++){
                for(int m = 0;m <col;m++){
                    d[n][m] = a[n][m] + b[n][m];
                }
            }
            return d;
        }else {
            return null;
        }
    
    } 
    
    
    static void matrixDisplay(double[][] a){
        int row=a.length;
        int col=a[0].length;
        for(int i = 0; i < row;i++){
            for(int k = 0;k < col;k++){
                System.out.print(a[i][k] + "\t");
            }
        System.out.println();
        }
    
    }
    

    public static void main(String[] args){ 双[][] A = {{1,2,3},{4,5,6}};

    double[][] B= {{1,2},{3,4},{5,6}};
    
    double[][] d;
    d = matrixMult(A,B);
    matrixDisplay(d);
    

    }

    }

    【讨论】:

      【解决方案5】:

      我不知道您使用 Java 多久了。在java中,使用数组,你必须

      1. create a block of memory for it and assign it to a reference
      double[][] matrix; // now, matrix == null
      matrix = new double[10][10]; // now matrix is the address of the memory
      2. initialize it 
      matrix[0][3]=0
      3. access it
      System.out.println(matrix[0][3])
      

      在您的情况下,C 和 d 都在上述第一步中声明,没有为其分配任何内存块并保留为 null,这意味着当您初始化或访问它时,您将触发空指针异常

      double[][] C; //declar this variable for return the result return null if on of matrix are null
      double[][] 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

      【讨论】:

      • 谢谢你的声明,我是java新手。
      猜你喜欢
      • 2011-05-09
      • 2012-03-25
      • 2015-07-04
      • 1970-01-01
      相关资源
      最近更新 更多