小编Kas*_*ami的帖子

返回ArrayIntList的方法

我正在研究以下问题:

编写一个方法runningTotal,它返回一个新的ArrayIntList,它包含原始列表的运行总计.换句话说,新列表中的第i个值应该存储原始列表的元素0到i的总和.

我陷入了第二种方法的最后一部分(返回).没有参数的方法.

public class ArrayIntList {

    private int[] elementData;
    private int size;

}

// when client calls : test = ArrayIntList.runningTotal(test);
// the folowing method works fine

public static ArrayIntList runningTotal(ArrayIntList other) {

    other.elementData[0] = other.elementData[0];
    for(int i = 1; i < other.size; i++){
        other.elementData[i] = other.elementData[i]+ other.elementData[i-1];
    }
    return other;
}
// when client calls: test = test.runningTotal();
public ArrayIntList runningTotal() {
    elementData[0] = elementData[0];
    for(int i = 1; i < size; i++){
        elementData[i] = elementData[i]+ elementData[i-1];
    } …
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
1121
查看次数

标签 统计

java ×1