5 回答

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);
}
}

TA貢獻1770條經驗 獲得超3個贊
您應該在使用它們之前初始化變量 C 和 d,因為 Java 將數組視為對象并且它們被分配在堆內存中。
double[][] C = new double[A.length][B[1].length]
double[][] d = new double[a.length][b[1].length]

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

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);
}
}

TA貢獻1828條經驗 獲得超4個贊
由于您的matrixMult
方法中有條件語句,因此有可能永遠不會滿足條件。所以你的聲明
double[][] C;
可能永遠不會被初始化,這將導致錯誤
return C;
所以你可以通過初始化它來修復它,例如
double[][] C = new double[length][length]; // your actual dimensions
除此之外,您C
在 if 語句中缺少數組的分配語句。您不能將值分配給未初始化的數組。
添加回答
舉報