Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update the last element of List

I have a List

val first = List("A","B","C","D")

and I want to create a new list from it but change the last element only:

val newLastVal = "E"
val second = List("A","B","C","E")

can't figure this one out! Thanks in advance

like image 315
Zuriar Avatar asked Jul 19 '14 09:07

Zuriar


1 Answers

you can also use .init or .dropRight(1) to remove last element and then can add new item to list

val second=first.init:+newLastVal //preferable 

OR

val second=first.dropRight(1):+newLastVal
like image 117
Govind Singh Avatar answered Sep 22 '22 15:09

Govind Singh