Replace element in List with scala

后端 未结 7 1124
花落未央
花落未央 2021-02-01 00:45

How do you replace an element by index with an immutable List.

E.g.

val list = 1 :: 2 ::3 :: 4 :: List()

list.replace(2, 5)
7条回答
  •  星月不相逢
    2021-02-01 01:10

    You can use list.updated(2,5) (which is a method on Seq).

    It's probably better to use a scala.collection.immutable.Vector for this purpose, becuase updates on Vector take (I think) constant time.

提交回复
热议问题