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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With