Replace element in List with scala

If you want to replace index 2, then

list.updated(2,5)    // Gives 1 :: 2 :: 5 :: 4 :: Nil

If you want to find every place where there’s a 2 and put a 5 in instead,

list.map { case 2 => 5; case x => x }  // 1 :: 5 :: 3 :: 4 :: Nil

In both cases, you’re not really “replacing”, you’re returning a new list that has a different element(s) at that (those) position(s).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)