scala - What is the difference between val b=a (a is an Array) and val b=a.clone()? -


I am reading scaladocs and am thinking the difference between direct assignment and .clone method.

  val a = array (1,2,3,4,5)   

case 1:

  val B = a   

Case 2:

  val b = a.clone ()    

Consider:

  scala> Val a = array (1,2,3,4,5) a: array [int] = array (1, 2, 3, 4, 5) scale & gt; Well B = A B: Array [int] = Array (1, 2, 3, 4, 5) Scale & gt; Val c = a.clone () c: Array [Int] = Array (1, 2, 3, 4, 5) Scale & gt; B (0) = 0 Scala & gt; C (1) = 1 Scala & gt; A res2: array [int] = array (0, 2, 3, 4, 5) scale & gt; BR3: Array [int] = array (0, 2, 3, 4, 5) scale & gt; C rays 4: array [int] = array (1, 1, 3, 4, 5)   

As you can see, when you val b = a , then a and b point to the same object when the object changes, the change will be seen by both.

On the other hand, when you clone the array, you create a new array with the same content. Changing this new array does not change the old.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -