Scala 中 :: 和 ::: 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6566502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between :: and ::: in Scala
提问by Hesey
val list1 = List(1,2)
val list2 = List(3,4)
then
然后
list1::list2 returns:
List[Any] = List(List(1, 2), 3, 4)
list1:::list2 returns:
List[Int] = List(1, 2, 3, 4)
I saw the book writes that when use ::it also results List[Int] = List(1, 2, 3, 4). My Scala version is 2.9.
我看书上写着用的::时候也是有效果的List[Int] = List(1, 2, 3, 4)。我的 Scala 版本是 2.9。
回答by Debilski
::prepends a single item whereas :::prepends a complete list. So, if you put a Listin front of ::it is taken as one item, which results in a nested structure.
::前置单个项目,而:::前置一个完整列表。所以,如果你List在::它前面放了一个就被当作一个项目,这会导致一个嵌套结构。
回答by RefaelJan
In general:
一般来说:
::- adds an element at the beginning of a list and returns a list with the added element:::- concatenates two lists and returns the concatenated list
::- 在列表的开头添加一个元素并返回一个包含添加元素的列表:::- 连接两个列表并返回连接后的列表
For example:
例如:
1 :: List(2, 3) will return List(1, 2, 3)
List(1, 2) ::: List(3, 4) will return List(1, 2, 3, 4)
In your specific question, using ::will result in list in a list (nested list) so I believe you prefer to use :::.
在您的具体问题中, using::将导致列表中的列表(嵌套列表),因此我相信您更喜欢使用:::.
Reference: class List int the official site

