第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

變量可能尚未在二維數(shù)組中初始化

變量可能尚未在二維數(shù)組中初始化

嚕嚕噠 2022-10-07 17:39:11
我有一個(gè)靜態(tài) matrixMult 和 static matrixAdd 方法和一個(gè)靜態(tài) matrixDisplay(用于打印結(jié)果),我在 Main 函數(shù)中編寫了一個(gè)測(cè)試示例。我的目標(biāo):我想用這兩個(gè)靜態(tài)方法相乘和相加兩個(gè)矩陣。我收到這些錯(cuò)誤“變量 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貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

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


對(duì)于 C - double[][] C = new double[A.length][B.length];


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

}


}



查看完整回答
反對(duì) 回復(fù) 2022-10-07
?
德瑪西亞99

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊

您應(yīng)該在使用它們之前初始化變量 C 和 d,因?yàn)?Java 將數(shù)組視為對(duì)象并且它們被分配在堆內(nèi)存中。

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

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


查看完整回答
反對(duì) 回復(fù) 2022-10-07
?
牛魔王的故事

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊

不知道你用Java多久了。在java中,使用數(shù)組,你必須


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 ,沒有為其分配任何內(nèi)存塊并保留為空,這意味著當(dāng)您初始化或訪問它時(shí),您將觸發(fā)空指針異常


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

在您的情況下,請(qǐng)?zhí)砑右韵聝?nèi)容以修復(fù)它。


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

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

Java 數(shù)組的使用,這可能是一個(gè)很好的例子 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html


查看完整回答
反對(duì) 回復(fù) 2022-10-07
?
HUH函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

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


雙[][] C=新雙[A.length][B[0].length]; 雙[][] d=新雙[b.length][b[0].length];


同樣在您的代碼中的許多地方,您使用基于 1 的索引來測(cè)量 2D 數(shù)組中的列數(shù),這可能導(dǎo)致違反內(nèi)存訪問(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);

}


}


查看完整回答
反對(duì) 回復(fù) 2022-10-07
?
明月笑刀無情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

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

 double[][] C;

可能永遠(yuǎn)不會(huì)被初始化,這將導(dǎo)致錯(cuò)誤

return C;

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

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

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


查看完整回答
反對(duì) 回復(fù) 2022-10-07
  • 5 回答
  • 0 關(guān)注
  • 172 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)