Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Patch function

object TestScala {
def main(args: Array[String]): Unit = {
val mainList = List(3, 2, 1)
mainList.patch(1, Seq(5), 0)
println("mainList-->"+mainList)
 }
}

The output is mainList-->List(3, 2, 1) not expected one

I am expecting 3,5,2,1

like image 894
satheesh S Avatar asked Feb 04 '26 08:02

satheesh S


1 Answers

Scala List is immutable. Assign it to variable and it will produce desired output.

object TestScala {
def main(args: Array[String]): Unit = {
var mainList = List(3, 2, 1)
mainList  = mainList.patch(1, Seq(5), 0)
println("mainList-->"+mainList)
 }
}
like image 98
undefined_variable Avatar answered Feb 07 '26 00:02

undefined_variable



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!