有人可以向我解释5行返回编号清单[index]&#x2B的细节是什么。 sum(numberSlist,index -1);
有人可以解释语法过程吗? 如何实现第5行 ,这三个元素的序列是什么?下面的飞镖代码段中的每次值是多少?
- numberSlist [index]
- sum(numberSlist,
- index -1);
int sum(List<int> numberList, int index) {
if (index < 0) {
return 0;
} else {
return numberList[index] + sum(numberList, index - 1);
}
}
main() {
// Driver Code
var result = sum([1, 2, 3, 4, 5], 4);
print(result);
}
Can someone explain the syntax process? How line 5 will be implemented and what is the sequence for these three elements and what is the value each time in the dart code snippet below?
- numberList[index]
- sum(numberList,
- index - 1);
int sum(List<int> numberList, int index) {
if (index < 0) {
return 0;
} else {
return numberList[index] + sum(numberList, index - 1);
}
}
main() {
// Driver Code
var result = sum([1, 2, 3, 4, 5], 4);
print(result);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在执行代码时,我将尝试显示这些元素的顺序(在下面的图像中执行(单击图像以正确的大小)):
0“总和调用的响应正在返回结果并向左传播。
希望这对您
进行编辑:
我添加了以下图像,以显示结果如何返回到第一个总和。
I will try to show the sequence for these elements, when the code is executed, in the following image (click on the image to see it in correct size):
As you can see, when the last call "returns 0" the response of the sum calls are returning the result and propagating to the left.
I hope this help you
Edited:
I've added the following image, to show how the result is being returned until the first call of sum.