list 将元素添加到 Scala 中的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19610320/
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
Add element to a list In Scala
提问by Emmanuel
I'm running Scala 2.10.2. I want to create a list, then add some elements to the list and expect to see all the elements in the lists when I call the list's name. But I observed something quite weird (At least weird for me since I'm a newbie). Below is the what I tried to do in my sbt console
我正在运行 Scala 2.10.2。我想创建一个列表,然后将一些元素添加到列表中,并期望在我调用列表名称时看到列表中的所有元素。但是我观察到一些很奇怪的东西(至少对我来说很奇怪,因为我是新手)。以下是我尝试在我的sbt console
scala> val l = 1.0 :: 5.5 :: Nil
l: List[Double] = List(1.0, 5.5)
scala> l
res0: List[Double] = List(1.0, 5.5)
scala> l ::: List(2.2, 3.7)
res1: List[Double] = List(1.0, 5.5, 2.2, 3.7)
scala> List(l) :+ 2.2
res2: List[Any] = List(List(1.0, 5.5), 2.2)
scala> l
res3: List[Double] = List(1.0, 5.5)
scala>
First, I created the list l
with 2 elements (1.0 and 5.5). I call l
and get what I expect; the two elements. Now I tried to add another element to the list using :::
which returned a new list with a new list of elements I added (2.2 and 3.7) Sweet! I even checked someone else's code for help: Appending an element to the end of a list in Scalato use a new construct :+
. So at this stage I'm all happy, but I call l
and I get the unexpected: `res3: List[Double] = List(1.0, 5.5)'.
首先,我创建了l
包含 2 个元素(1.0 和 5.5)的列表。我打电话l
并得到我所期望的;这两个元素。现在我尝试将另一个元素添加到列表中,使用:::
它返回一个新列表,其中包含我添加的新元素列表(2.2 和 3.7) 甜!我什至检查了其他人的代码以寻求帮助:在 Scala 中将元素附加到列表的末尾以使用新的构造:+
。所以在这个阶段我很高兴,但我打电话给了l
我意想不到的结果:`res3: List[Double] = List(1.0, 5.5)'。
Where are the elements I added? And how do I add these elements correctly so that when I call l
I get a new list with all the stuff I added?
我添加的元素在哪里?我如何正确添加这些元素,以便在我打电话时l
得到一个包含我添加的所有内容的新列表?
回答by Rich Oliver
You are using an immutable list. The operations on the List return a new List. The old List remains unchanged. This can be very useful if another class / method holds a reference to the original collection and is relying on it remaining unchanged. You can either use different named vals as in
您正在使用不可变列表。List 上的操作返回一个新的 List。旧列表保持不变。如果另一个类/方法持有对原始集合的引用并依赖它保持不变,这将非常有用。您可以使用不同的命名 val,如
val myList1 = 1.0 :: 5.5 :: Nil
val myList2 = 2.2 :: 3.7 :: mylist1
or use a var as in
或使用 var 作为
var myList = 1.0 :: 5.5 :: Nil
myList :::= List(2.2, 3.7)
This is equivalent syntax for:
这是等效的语法:
myList = myList.:::(List(2.2, 3.7))
Or you could use one of the mutable collections such as
或者您可以使用可变集合之一,例如
val myList = scala.collection.mutable.MutableList(1.0, 5.5)
myList.++=(List(2.2, 3.7))
Not to be confused with the following that does not modify the original mutable List, but returns a new value:
不要与以下不修改原始可变列表但返回新值的内容混淆:
myList.++:(List(2.2, 3.7))
However you should only use mutable collections in performance critical code. Immutable collections are much easier to reason about and use. One big advantage is that immutable List and scala.collection.immutable.Vectorare Covariant. Don't worry if that doesn't mean anything to you yet. The advantage of it is you can use it without fully understanding it. Hence the collection you were using by default is actually scala.collection.immutable.Listits just imported for you automatically.
但是,您应该只在性能关键代码中使用可变集合。不可变集合更容易推理和使用。一大优势是不可变 List 和scala.collection.immutable.Vector是协变的。如果这对您来说还没有任何意义,请不要担心。它的优点是您可以在不完全理解它的情况下使用它。因此,您默认使用的集合实际上是scala.collection.immutable.List它刚刚为您自动导入。
I tend to use List as my default collection. From 2.12.6 Seq defaults to immutable Seq prior to this it defaulted to immutable.
我倾向于使用 List 作为我的默认集合。从 2.12.6 Seq 默认为不可变 Seq 在此之前它默认为不可变。
回答by Anthony Accioly
Use import scala.collection.mutable.MutableListor similar if you really need mutation.
如果您确实需要突变,请使用import scala.collection.mutable.MutableList或类似工具。
import scala.collection.mutable.MutableList
val x = MutableList(1, 2, 3, 4, 5)
x += 6 // MutableList(1, 2, 3, 4, 5, 6)
x ++= MutableList(7, 8, 9) // MutableList(1, 2, 3, 4, 5, 6, 7, 8, 9)
回答by Yann Moisan
I will try to explain the results of all the commands you tried.
我将尝试解释您尝试过的所有命令的结果。
scala> val l = 1.0 :: 5.5 :: Nil
l: List[Double] = List(1.0, 5.5)
First of all, List
is a type alias to scala.collection.immutable.List
(defined in Predef.scala).
首先,List
是一个类型别名scala.collection.immutable.List
(在 Predef.scala 中定义)。
Using the List companion object is more straightforward way to instantiate a List
. Ex: List(1.0,5.5)
使用 List 伴随对象是实例化List
. 前任:List(1.0,5.5)
scala> l
res0: List[Double] = List(1.0, 5.5)
scala> l ::: List(2.2, 3.7)
res1: List[Double] = List(1.0, 5.5, 2.2, 3.7)
:::
returns a list resulting from the concatenation of the given list prefix and this list
:::
返回由给定列表前缀和此列表连接产生的列表
The original List is NOT modified
原始列表未修改
scala> List(l) :+ 2.2
res2: List[Any] = List(List(1.0, 5.5), 2.2)
List(l)
is a List[List[Double]]
Definitely not what you want.
List(l)
是一种List[List[Double]]
绝对不是你想要的。
:+
returns a new list consisting of all elements of this list followed by elem.
:+
返回一个新列表,该列表由该列表的所有元素后跟 elem 组成。
The type is List[Any]
because it is the common superclass between List[Double]
and Double
类型是List[Any]
因为它是List[Double]
和之间的公共超类Double
scala> l
res3: List[Double] = List(1.0, 5.5)
l is left unmodified because no method on immutable.List
modified the List.
l 未修改,因为没有immutable.List
修改列表的方法。
回答by Aaditya Shukla
Since you want to append elements to existing list, you can use var List[Int] and then keep on adding elements to the same list. Note -> You have to make sure that you insert an element into existing list as follows:-
由于您想将元素附加到现有列表,您可以使用 var List[Int] 然后继续向同一列表添加元素。注意 -> 您必须确保将元素插入现有列表中,如下所示:-
var l: List[int] = List() // creates an empty list
var l: List[int] = List() // 创建一个空列表
l = 3 :: l // adds 3 to the head of the list
l = 3 :: l // 将 3 添加到列表的头部
l = 4 :: l // makes int 4 as the head of the list
l = 4 :: l // 使 int 4 作为列表的头部
// Now when you will print l, you will see two elements in the list ( 4, 3)
// 现在当你打印 l 时,你会看到列表中有两个元素 (4, 3)