亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

變量可能尚未在二維數組中初始化

變量可能尚未在二維數組中初始化

嚕嚕噠 2022-10-07 17:39:11
我有一個靜態 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);}}
查看完整描述

5 回答

?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

您必須初始化這些變量。在您的情況下,它將類似于:


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


對于 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);

}


}



查看完整回答
反對 回復 2022-10-07
?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

您應該在使用它們之前初始化變量 C 和 d,因為 Java 將數組視為對象并且它們被分配在堆內存中。

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

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


查看完整回答
反對 回復 2022-10-07
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

不知道你用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 ,沒有為其分配任何內存塊并保留為空,這意味著當您初始化或訪問它時,您將觸發空指針異常


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


查看完整回答
反對 回復 2022-10-07
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您尚未初始化數組變量的維度,因此編譯器將不知道需要分配數組的大小。


雙[][] 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) {

    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){ double[][] A = {{1,2,3},{4,5,6}};


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


double[][] d;

d = matrixMult(A,B);

matrixDisplay(d);

}


}


查看完整回答
反對 回復 2022-10-07
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

由于您的matrixMult方法中有條件語句,因此有可能永遠不會滿足條件。所以你的聲明

 double[][] C;

可能永遠不會被初始化,這將導致錯誤

return C;

所以你可以通過初始化它來修復它,例如

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

除此之外,您C在 if 語句中缺少數組的分配語句。您不能將值分配給未初始化的數組。


查看完整回答
反對 回復 2022-10-07
  • 5 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號