我是新來的java和我正在學習創建對象類。我的一項家庭作業要求我在相同對象類的方法中至少調用一次構造函數。我收到說The method DoubleMatrix(double[][]) is undefined for the type DoubleMatrix
從同一個類中的方法調用構造函數
這裏的錯誤是我的構造函數:
public DoubleMatrix(double[][] tempArray)
{
// Declaration
int flag = 0;
int cnt;
// Statement
// check to see if doubArray isn't null and has more than 0 rows
if(tempArray == null || tempArray.length < 0)
{
flag++;
}
// check to see if each row has the same length
if(flag == 0)
{
for(cnt = 0; cnt <= tempArray.length - 1 || flag != 1; cnt++)
{
if(tempArray[cnt + 1].length != tempArray[0].length)
{
flag++;
}
}
}
else if(flag == 1)
{
makeDoubMatrix(1, 1);// call makeDoubMatrix method
}
}// end constructor 2
這裏就是我嘗試調用構造函數的方法:
public double[][] addMatrix(double[][] tempDoub)
{
// Declaration
double[][] newMatrix;
int rCnt, cCnt;
//Statement
// checking to see if both are of same dimension
if(doubMatrix.length == tempDoub.length &&
doubMatrix[0].length == tempDoub[0].length)
{
newMatrix = new double[doubMatrix.length][doubMatrix[0].length];
// for loop to add matrix to a new one
for(rCnt = 0; rCnt <= doubMatrix.length; rCnt++)
{
for(cCnt = 0; cCnt <= doubMatrix.length; cCnt++)
{
newMatrix[rCnt][cCnt] = doubMatrix[rCnt][cCnt] + tempDoub[rCnt][cCnt];
}
}
}
else
{
newMatrix = new double[0][0];
DoubleMatrix(newMatrix)
}
return newMatrix;
}// end addMatrix method
能有人指出正確的方向並解釋爲什麼我得到一個錯誤?
中的其他部分添加新的關鍵字...其他 { newMatrix =新的雙[0] [0]; 新的DoubleMatrix(newMatrix) } – Vikram
@asvikki謝謝,這使錯誤消失。你能向我解釋爲什麼這項工作? – Nathan
請參閱下面的答案 – Vikram