How to replace this item in the list?

This describes the problem very 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 ^ 
+6
list scala
source share
5 answers

scala.List is immutable, meaning you cannot update it in place. If you want to create a copy of your list that contains an updated mapping, you can do the following:

 val updated = l2.updated( 2, 55 ) 

There are also modified ordered sequence types in scala.collection.mutable , such as Buffer types, which are more like what you want. If you try the following, you should have more success:

 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 note that some other answers mentioned that you should use a “mutable list type” - this is true, but the “List” in Scala only refers to a singly linked list, whereas in Java it is usually used for any ordered collection with arbitrary access. There is also a DoubleLinkedList , which is more like a Java LinkedList and a MutableList , which is the type used for the internal components of some other types.

All in all, what you might want in Scala is the Buffer for this job; especially since the default implementation is an ArrayBuffer , which is almost the same as an ArrayList , the default for most people in Java.

If you ever want to find out what the closest “mapping” of the Java collections interface to the Scala world is, however, the easiest way to check is JavaConversions . In this case, you can see that the mapping is Buffer :

 scala.collection.mutable.Buffer <=> java.util.List 
+26
source share

Your problem is that lists in scala are immutable; instead, you need to use a modified list. Import scala.collection.mutable.Queue and use this instead. Queue is a MutableList, so it should do what you want.

+2
source share

The list you create 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.

+2
source share

But you can wrap an assignment implicitly if you don't like 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) = 3 List(1, 3, 3) 
+2
source share

Lists do not change in scala. But if you want to replace an item in a list, you can use an “updated” method like this

 val num:List[Int]=List(1,5,4,7,8,2,10) num=num.updated(0,22) 

Since the lists are immutable, this creates a copy of the first list named num (since we assigned the new list “num”), replacing the 0th element with 22. So genaral updated method is

 listname.updated(index,value) 
-one
source share

All Articles