list 如何替换列表中的给定项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5094314/
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
How to replace a given item in a list?
提问by Ska
This describes the problem pretty well:
这很好地描述了问题:
scala> var l2 = List(1,2,3)
l2: List[Int] = List(1, 2, 3)
scala> l2(2) = 55
<console>:10: error: value update is not a member of List[Int]
l2(2) = 55
^
回答by Calum
scala.List
is immutable, meaning you cannot update it in place. If you want to create a copyof your List which contains the updated mapping, you can do the following:
scala.List
是不可变的,这意味着您无法就地更新它。如果要创建包含更新映射的列表副本,可以执行以下操作:
val updated = l2.updated( 2, 55 )
There are mutable ordered sequence types as well, in scala.collection.mutable
, such as Buffer
types which seem more like what you want. If you try the following you should have more success:
也有可变的有序序列类型 in scala.collection.mutable
,例如Buffer
看起来更像您想要的类型。如果您尝试以下操作,您应该会取得更大的成功:
scala> import scala.collection._
import scala.collection._
scala> val b = mutable.Buffer(1,2,3)
b: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3)
scala> b(2) = 55
scala> b
res1: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 55)
Edit: Just to note that some other answers have mentioned that you should use a "mutable List type" - this is true, but "List" in Scala just refers to the single-linked list, whereas in Java it's generally used for any ordered, random-access collection. There is also a DoubleLinkedList
, which is more like a Java LinkedList
, and a MutableList
, which is a type used for the internals of some other types.
编辑:请注意,其他一些答案已经提到您应该使用“可变列表类型” - 这是真的,但 Scala 中的“列表”仅指单链表,而在 Java 中,它通常用于任何有序,随机访问集合。还有一个DoubleLinkedList
,它更像是一个 Java LinkedList
,和一个MutableList
,它是一种用于其他一些类型内部的类型。
Generally speaking what you probably want in Scala is a Buffer
for this job; especially since the default implementation is an ArrayBuffer
, which is pretty close to being the same as ArrayList
, most peoples' default, in Java.
一般来说,你在 Scala 中可能想要的是一份Buffer
这份工作;特别是因为默认实现是 an ArrayBuffer
,这与ArrayList
Java 中大多数人的默认实现非常接近。
If you ever want to find out what the closest "mapping" of a Java collections interface to the Scala world is, though, the easiest thing to do is probably just check what JavaConversionsdoes. In this case you can see the mapping is to Buffer
:
但是,如果您想知道 Java 集合接口与 Scala 世界最接近的“映射”是什么,最简单的方法可能就是检查JavaConversions 的作用。在这种情况下,您可以看到映射为Buffer
:
scala.collection.mutable.Buffer <=> java.util.List
回答by Mika?l Mayer
But you can wrap the assignment in an implicit if you don't like updated
但是如果你不喜欢,你可以用隐式包装 updated
implicit class RichList[A](l: List[A]) {
def update(which: Int, what: A): List[A] = {
l.updated(which, what)
}
}
val l = List(1, 2, 3)
val l2 = l(2) = 55
List(1, 2, 55)
回答by Janx
The List you are creating is immutable.
您正在创建的列表是不可变的。
scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)
scala> l.getClass
res3: java.lang.Class[_] = class scala.collection.immutable.$colon$colon
Use a mutable List instead and you should be fine.
改用可变列表,你应该没问题。
回答by Tim Blair
Your problem is that Lists in scala are immutable, you need to use a mutable list instead. Import scala.collection.mutable.Queue and use that instead. Queue is a MutableList, so it should do what you want.
您的问题是 Scala 中的列表是不可变的,您需要改用可变列表。导入 scala.collection.mutable.Queue 并使用它。Queue 是一个 MutableList,所以它应该做你想做的。
回答by Nishadi Wickramanayaka
Lists are immutable in scala. But if you want to replace an element in a List you can use "updated" method like this
列表在 Scala 中是不可变的。但是如果你想替换 List 中的一个元素,你可以使用这样的“更新”方法
val num:List[Int]=List(1,5,4,7,8,2,10)
num=num.updated(0,22)
Since Lists are immutable,this creates a copy of the first List called num(as we assigned the new list to 'num') by replacing 0th element to 22.So the genaral updated method is
由于列表是不可变的,这通过将第 0 个元素替换为 22 来创建名为 num 的第一个列表的副本(因为我们将新列表分配给了 'num')。所以一般的更新方法是
listname.updated(index,value)