在Scala中将元素添加到列表 [英] Add element to a list In Scala

查看:1221
本文介绍了在Scala中将元素添加到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Scala 2.10.2.我想创建一个列表,然后向列表中添加一些元素,并希望在调用列表名称时看到列表中的所有元素.但是我发现有些奇怪(至少对我来说很奇怪,因为我是新手).以下是我在sbt console

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> 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> 

首先,我创建了包含2个元素(1.0和5.5)的列表l.我打电话给l并得到了期望这两个要素.现在,我尝试使用:::将另一个元素添加到列表中,这返回了一个新列表,其中包含我添加的新元素列表(2.2和3.7),甜心!我什至检查了别人的代码以寻求帮助:附加Scala中列表末尾的元素,以使用新的构造:+.因此,在这个阶段我很高兴,但是我打电话给l,却得到了意外的消息:"res3:List [Double] = List(1.0,5.5)".

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 Scala to 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时获得包含所有添加内容的新列表?

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?

推荐答案

您正在使用不可变列表.列表上的操作将返回一个新的列表.旧列表保持不变.如果另一个类/方法持有对原始集合的引用并依赖于其保持不变,则这将非常有用.您可以像在

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

val myList1 = 1.0 :: 5.5 :: Nil 
val myList2 = 2.2 :: 3.7 :: mylist1

或使用

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))

但是,您只应在对性能有严格要求的代码中使用可变集合.不可变的集合更易于推理和使用.一大优势是,不可变列表和 scala.collection.immutable .Vector 是协变的.如果这对您没有任何意义,请不要担心.它的优点是您可以在不完全了解它的情况下使用它.因此,您默认使用的集合实际上是 scala.collection .immutable.List 会自动为您导入.

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.Vector are 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.List its just imported for you automatically.

我倾向于使用列表作为默认集合.从2.12.6 Seq默认设置为不可变Seq,在此之前,其默认设置为不可变.

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.

这篇关于在Scala中将元素添加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆