I have created a one-dimensional array(vector) in Julia, namely, a=[1, 2, 3, 4, 5]
. Then I want to create a new vector b
, where b
has exactly same elements in a
, i.e b=[1, 2, 3, 4, 5]
.
It seems that directly use b = a
just create a pointer for the original collection, which means if I modify b
and a
is mutable, the modification will also be reflected in a
. For example, if I use !pop(b)
, then b=[1, 2, 3, 4]
and a=[1, 2, 3, 4]
.
I am wondering if there is a official function to merely copy or clone the collection, which the change in b
will not happen in a
. I find a solution is use b = collect(a)
. I would appreciate that someone provide some other approaches.
b=copy(a)
Should do what you want.
methods(copy)
will give you a list of methods forcopy
, which will tell you what types ofa
this will work for.You can use the
copy
anddeepcopy
functions:Like this:
Notice that the help system hints of the existence of other copy related functions.