Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you can use <code>clone</code> method as well !!</p> <pre><code>def copy(bild: Array[Array[Int]]): Unit = { val copy = bild.clone } </code></pre> <p><strong>Update :</strong></p> <p>since, Array[Int] is still mutable references, cloning will still not solve the problem.. as mentioned by Andriy Plokhotnyuk in his comment..</p> <p><em>Problem :</em> </p> <pre><code>val og = Array(Array(1, 2, 3), Array(4,5,6)) //&gt; og : Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6)) val copy = og.clone //&gt; copy : Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6)) copy(0)(0) = 7 og //&gt; res2: Array[Array[Int]] = Array(Array(7, 2, 3), Array(4, 5, 6)) copy //&gt; res3: Array[Array[Int]] = Array(Array(7, 2, 3), Array(4, 5, 6)) </code></pre> <p>Here any update to <code>copy</code> will reflect to <code>og</code> also..</p> <p><em>Sol :</em></p> <p>So I primarily need to clone Array[Int] as well.. hence..</p> <pre><code>val og = Array(Array(1, 2, 3), Array(4,5,6)) //&gt; og : Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6)) val copy = og.map(_.clone) //&gt; copy : Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6)) copy(0)(0) = 7 og //&gt; res2: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6)) copy //&gt; res3: Array[Array[Int]] = Array(Array(7, 2, 3), Array(4, 5, 6)) </code></pre> <p>Hence.. Refactoring copy method in the question to..</p> <pre><code>def copy(bild: Array[Array[Int]]): Unit = { val copy = bild.map(_.clone) } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload