2017-08-09 52 views

回答

0

在Java subList(int x, int y)中將返回從x元素到y-1元素的列表。 對於list = [0,1,2,3],list.subList(1, 3)將返回[1,2]。

它會正確地爲您的功能工作。 例如:

Integer count(List<Integer> list) { 
    if (list.size() == 1) return list.get(0); 
    int index = list.size()/2; 
    return count(list.subList(0, index)) + count(list.subList(index, list.size())); 
} 
+0

謝謝!這絕對有幫助。出於某種原因,數組的第一個元素未打印。我這樣做,它解決了這個問題。但我仍然不知道究竟發生了什麼。我很高興它的工作! array.add(0,temp); System.out.printf(「遞歸結果:$%.2f」,count(數組)); – flora

+0

請添加您的代碼 - 這將更容易排除故障。 – Nequeq

相關問題