矩阵数组错误

时间:2015-04-09 10:17:03

标签: java arrays

我有以下代码:

double[][] Coeficientes;
double[] VectorCoeficientes = new double[13];

int z = 0;
for(int i=0; i<longVector-LventanaReal; i+=saltos){
    VectorCoeficientes = CalcularCoeficientes(i);  (Step1)
    Coeficientes[z] = VectorCoeficientes;   (Step2)
    z++;
}

CalcularCoeficientes给了我一个长度为13的数组,然后Step1运行良好但我不能执行Step2,我想将该数组保存在矩阵Coeficientes中。

1 个答案:

答案 0 :(得分:2)

您错过了Coeficientes数组的初始化。

改变:

double[][] Coeficientes;

到:

double[][] Coeficientes = new double[someLength][]; // where someLength is some
                                                    // int value that determines
                                                    // the number of rows in your
                                                    // matrix

只有这样你才能运行作业:

Coeficientes[z] = VectorCoeficientes;

如果行数未知,请使用List<double[]>并将List转换为矩阵:

double[][] Coeficientes;
List<double[]> temp = new ArrayList<>();

for(int i=0; i<longVector-LventanaReal; i+=saltos){
    double[] VectorCoeficientes = CalcularCoeficientes(i);
    temp.add(VectorCoeficientes);
}

Coeficientes = temp.toArray(new double[temp.size()][13]);