2013-02-25 68 views
1

我的工作:中的方法對以下問題返回ArrayIntList

編寫返回包含運行總計原始列表的新ArrayIntList的方法runningTotal。換句話說,新列表中的第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]; 
    } 
    return ??; 
} 

回答

5

我想你需要的是return this;

+0

完美!謝謝你的幫助 :) – 2013-02-25 06:07:17