一个数组,其中的项是另一个数组中连续项(相邻)的差异

时间:2015-04-14 05:41:30

标签: java arrays double

我目前有:

    public double[] differences()
    {
     diffs = new doublae[sequence.length-1];
     for (int = 0; i<sequence.length; ++i){
     double diff = sequence[i+1] - sequence[i];
     diffs[i] = diff;
    }
    return diffs;
    }

但是,当我运行测试程序来检查它时,这不起作用。

1 个答案:

答案 0 :(得分:1)

您的范围应该不同 - i不应该通过sequence.length - 2,以使i+1成为有效的索引。

double[] diffs = new double[sequence.length-1];
for (int = 0; i < sequence.length - 1; i++) { // changed the condition
 double diff = sequence[i+1] - sequence[i];
 diffs[i] = diff;
}