gpt4 book ai didi

list - 在Dart中返回[…list]有什么用?

转载 作者:行者123 更新时间:2023-12-03 04:42:24 25 4
gpt4 key购买 nike

在Dart编程语言中,有时我看到函数返回带有三点[...list]的方括号对的列表。例如:

class IntList {
List<int> _integerList = [1, 2, 3, 4, 5];
List<int> get integerList {
return [..._integerList];
}
}
那么 return integerList;和上面的return语句有什么区别?
任何帮助都非常感谢。谢谢。

最佳答案

对于这种特殊情况,没有区别。 ...是传播运算符。这使您可以将多个元素插入到集合中。
例如:

var list = [1, 2, 3];
var list2 = [0, ...list];
print(list2)

Output:
[0, 1, 2, 3]
因此,执行 return [..._integerList];与return integerList;完全相同,除了这样做会创建新列表。

var list = [1, 2, 3];
print(list.hashCode);
print([...list].hashCode);
此代码显示,由于输出了不同的哈希码,因此使用散布运算符时,它们是不同的 List对象。

关于list - 在Dart中返回[…list]有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62976173/

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