[英]Finding the Minimum of an Array List Using Recursive Functions
唯一可以更改的部分是第 29-39 行。 評論已經作為說明給出,findMinimum 的方法簽名需要保持不變。 作業說明位於第 1 部分圖像的右側。
我需要幫助更改的唯一行是第 39 行,我不明白如何格式化遞歸。
當方法的參數僅調用 ArrayList 時,如何在查找最小值的同時創建一個遞歸語句來刪除列表中的最后一個值?
以下修復應該有效:
return Math.min(numbers.get(amount - 1), findMin(numbers.subList(0, amount - 1)));
也可以使用List::remove(int index)
因為此方法返回已刪除的元素:
// remove the last element and then check the remainder, input array is cleared finally
return Math.min(numbers.remove(amount - 1), findMin(numbers));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.