40397

Why do these two different ways of constructing an array produce different behaviors?

When I construct a 2 element array in two different ways(e.g. a and b, I get two different results when I add an element to one of the inner arrays. This also happens with append!. Based on the output after constructing each, I'd expect them to be exactly the same?

julia> a = [[],[]] 2-element Array{Array{Any,1},1}: Any[] Any[] julia> push!(a[1],1.0) 1-element Array{Any,1}: 1.0 julia> a 2-element Array{Array{Any,1},1}: Any[1.0] Any[] julia> b = fill([],2) 2-element Array{Array{Any,1},1}: Any[] Any[] julia> push!(b[1],1.0) 1-element Array{Any,1}: 1.0 julia> b 2-element Array{Array{Any,1},1}: Any[1.0] Any[1.0]

Answer1:

fill will create an array initialized with n copies (shallow) of the same object, so that b[1] === b[2], and when you update b[1], you're updating the same object that's also pointed to in b[2].

Answer2:

Take a look at the following examples. Hopefully the behavior is as you expect.

Input[1]:

a_1 = [] a_2 = [] a = [a_1, a_2] push!(a_1, 1.0) @show a_1 @show a

Output[1]:

a_1 = Any[1.0] a = Array{Any,1}[Any[1.0],Any[]]

Input[2]

push!(a[1], 2.0) @show a_1 #Guess what this shows @show a

Output[2]:

a_1 = Any[1.0,2.0] a = Array{Any,1}[Any[1.0,2.0],Any[]] <hr>

Input[3]:

b_n = [] b = fill(b_n, 2) push!(b_n, 1.0) @show b_n @show b

Output[3]:

b_n = Any[1.0] b = Array{Any,1}[Any[1.0],Any[1.0]]

Input[4]:

push!(b[1], 2.0) @show b_n #Guess what this shows @show b

Output[4]

b_n = Any[1.0,2.0] b = Array{Any,1}[Any[1.0,2.0],Any[1.0,2.0]]

Input[5]:

c_n = [] c = [c_n, c_n] push!(c_n, 1.0) @show c_n @show c

Output[5]:

c_n = Any[1.0] c = Array{Any,1}[Any[1.0],Any[1.0]]

Input[6]:

push!(c[1], 2.0) @show c_n @show c

Output[6]:

c_n = Any[1.0,2.0] c = Array{Any,1}[Any[1.0,2.0],Any[1.0,2.0]] <hr>

So Input[1] is the same as your a, and Input[3] is the same as your b

Each time you put a [] you construct a new Vector.

So in the first case: a=[[],[]] creates a vector containing two new vectors which I call a_1 and a_2 in Input[1]

In the second case: b=fill([],2] creates a vector, which I call b_n in Input[3], and then it fills an vector of length 2, with that vector b_n. This it equivelent to the example in Input[3] (with c). I might as well have said: [b_n, b_n] as said fill(b_n,2)

So it references to the same vector at each position. So changing one, changes the both.

Recommend

  • working with Nested IF statement in excel
  • Omit template arguments when create an instance of template class from another instance of template
  • Check if one value in one column is in another column
  • Use MongoDB array as stack
  • How to run a query between dates and times?
  • No error while instantiating abstract class, even though abstract method is not implemented
  • How to remove trailing space from marquee?
  • Update SQL MS Access 2010
  • How to edit css for jquery datepicker prev/next buttons?
  • Understanding Intl.DateTimeFormat as a JavaScript object
  • What do I do with this error when I run tests in rails?
  • Is it safe to drop the -webkit vendor prefix from the css3 border-radius yet?
  • MRO with multiple inheritance in python
  • How to concat Pandas dataframe columns
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • Angular Bootstrap Carousel Slide Transition not working correctly
  • several dataProvider per one Test in TestNG
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Excel's Macro-Recorder usage
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • Bad request using file_get_contents for PUT request in PHP
  • Why does access(2) check for real and not effective UID?
  • Retrieving value from sql ExecuteScalar()
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • Javascript + PHP Encryption with pidCrypt
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Comma separated Values
  • Matrix multiplication with MKL
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Java static initializers and reflection
  • Qt: Run a script BEFORE make
  • costura.fody for a dll that references another dll
  • Conditional In-Line CSS for IE and Others?
  • How to push additional view controllers onto NavigationController but keep the TabBar?