gpt4 book ai didi

java - 返回 ArrayIntList 的方法

转载 作者:行者123 更新时间:2023-11-29 07:59:50 26 4
gpt4 key购买 nike

我正在研究以下问题:

Write a method runningTotal that returns a new ArrayIntList that contains a running total of the original list. In other words, the i th value in the new list should store the sum of elements 0 through i of the original list.

我卡在了第二种方法的最后一部分(返回)。没有参数的方法。

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];
}
return ??;
}

最佳答案

我想你需要的是return this;

关于java - 返回 ArrayIntList 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15060995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com