How do I do a deep copy of a 2D array in Scala?
For example
val a = Array[Array[Int]](2,3)
a(1,0) = 12
I want val b to copy values of a but without pointing to the same array.
You can use the clone
method of the Array
class. For a multi-dimensional Array
, use map
on the extra dimensions. For your example, you get
val b = a.map(_.clone)
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