Julia: 引用、copy与数组

和MATLAB不一样,JULIA在数组中是引用的,而不是值拷贝。如果要值拷贝,就要用copy().

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.5.0-dev+566 (2015-10-01 22:40 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit fdde0bd (27 days old master)
|__/                   |  x86_64-w64-mingw32

julia> s=Dict{ASCIIString,Array{Any,1}}()
Dict{ASCIIString,Array{Float64,1}} with 0 entries

julia> c =Any[1,2]
2-element Array{Float64,1}:
 1.0
 2.0

julia> setindex!(s,c,"a")
Dict{ASCIIString,Array{Any,1}} with 1 entry:
  "a" => [1.0,2.0]

julia> push!(c,11)
3-element Array{Any,1}:
  1.0
  2.0
 11.0

julia> s
Dict{ASCIIString,Array{Any,1}} with 1 entry:
  "a" => [1.0,2.0,11.0]

如果不是Any, 是Float64等其它类的Array{},也是一样的。

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.4.0 (2015-10-08 06:20 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-w64-mingw32

julia> a =[1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> b=a;

julia> push!(b,4)
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> a
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> c=copy(a)
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> push!(c,5)
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> a
4-element Array{Int64,1}:
 1
 2
 3
 4

你可能感兴趣的:(Julia)