在java中有效地重用数组

时间:2014-02-22 20:26:02

标签: java arrays

对于下面的程序,我想保持双数组c在另一个函数中重用。我不想使用ArrayList。如果有人对如何以有效的方式解决这个问题有任何想法,我将不胜感激。

编辑:我忘了提到双数组c将改变大小。有没有办法删除数组,然后以不同的大小重新创建数组?

public double[][] forwardAlgo(){
    int time = eSequence.size();
    double alpha[][] = new double[2][time];
    double c[] = new double[time];

    // Intialize alpha for time 0
    c[0] = 0; // c0 is for scaling purposes to avoid underflow
    for(int i = 0; i < 2; i ++){
        alpha[i][0] = pi[i] * emissionMatrix[i][eSequence.get(0)];
        c[0] = c[0] + alpha[i][0];
    }

    // Scale alpha_0
    c[0] = 1 / c[0];
    for(int i = 0; i < 2; i++){
        alpha[i][0] = c[0]*alpha[i][0];
        System.out.format("%f, ", alpha[i][0]);
    }

    // Use recursive method to calculate alpha
    double tempAlpha = 0;
    for(int t = 1; t < time; t++){
        c[t] = 0;
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 2; j++){
                tempAlpha = tempAlpha + alpha[j][t-1] * stateTransitionMatrix[j][i];
            }
            alpha[i][t] = tempAlpha * emissionMatrix[i][eSequence.get(t)];
            c[t] = c[t] + alpha[i][t];
            tempAlpha = 0;
        }
    }

    for(int t = 1; t < time; t++){
        c[t] = 1 / c[t];
        for(int i = 0; i < time; i++){
            alpha[i][t] = alpha[i][t] * c[t];
        }
    }

    return alpha;
}

0 个答案:

没有答案