Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep copy of 2D array in Scala?

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.

like image 355
Tom Jinaad Avatar asked Feb 03 '23 08:02

Tom Jinaad


1 Answers

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)
like image 189
Leo Avatar answered Feb 11 '23 23:02

Leo