scala - How to replace a given item in a list? -


this describes 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 not member of list[int]               l2(2) = 55               ^ 

scala.list immutable, meaning cannot update in place. if want create copy of list contains updated mapping, can following:

val updated = l2.updated( 2, 55 ) 

there mutable ordered sequence types well, in scala.collection.mutable, such buffer types seem more want. if try following 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: note other answers have mentioned should use "mutable list type" - true, "list" in scala refers single-linked list, whereas in java it's used ordered, random-access collection. there doublelinkedlist, more java linkedlist, , mutablelist, type used internals of other types.

generally speaking want in scala buffer job; since default implementation arraybuffer, pretty close being same arraylist, peoples' default, in java.

if ever want find out closest "mapping" of java collections interface scala world is, though, easiest thing check javaconversions does. in case can see mapping buffer:

scala.collection.mutable.buffer <=> java.util.list 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -