Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Why doesn't modifying a shallow copy of an array reflect on the original?

$
0
0

Suppose if I had the following array:

even_numbers = Int32[2,4,6,8,10] # Creating a 5 element array.

and I made a shallow copy:

b = copy(even_numbers)

If I were to modify or delete an element from b why doesn’t it reflect on even_numbers?

deleteat!(b, 2) # the element of 4 should be removed.

If I were to display even_numbers, 4 still exists.

5-element Array{Int32,1}:
  2
  4
  6
  8
 10

I always thought if you modify a shallow copy of a collection it will also modify the original collection. If you didn’t want to modify the original, you can use deepcopy()

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images