首先声明数组,稍后初始化

时间:2015-11-27 19:01:22

标签: java arrays

我遇到了问题,因为我需要从其他方法访问2d数组retArray[][],但数组的大小是在构造函数方法中确定的。将它置于构造函数方法之外会导致未定义theYearsNum并且现在放入它会导致无法从其他方法中看到它。有没有办法解决这个问题?

...

public Taxation(int theSalary, int theYear, int theYearsNum, double theLowTaxRate, double theHighTaxRate, int theBoundaryLimit){

    double retArray[][] = new double[theYearsNum][3];

}

// Methods

...

1 个答案:

答案 0 :(得分:4)

将其声明为成员并在构造函数中初始化它:

private double[][] retArray;
public Taxation(int theSalary, int theYear, int theYearsNum, double theLowTaxRate, double theHighTaxRate, int theBoundaryLimit)
{    
    retArray = new double[theYearsNum][3];    
}