# Need help with adding Composite types to Arrays

**URL:** <https://discourse.julialang.org/t/need-help-with-adding-composite-types-to-arrays/838>\
**Category:** New to Julia\
**Created:** [December 9, 2016, 8:57pm UTC](https://discourse.julialang.org/t/need-help-with-adding-composite-types-to-arrays/838 "2016-12-09T20:57:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aekiman](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Aekiman](https://discourse.julialang.org/u/Aekiman)\
**Post date:** [December 9, 2016, 8:57pm UTC](https://discourse.julialang.org/t/need-help-with-adding-composite-types-to-arrays/838/1 "2016-12-09T20:57:12Z")

</div>

Hello, I am new to Julia and need some help please.  
I want to use Composite types and add them to an Array.  
The following is a simple example:

```julia
type State
  x::Int64
end

s=State(1)
data = Array{State}(1)
data[1]=s
println(data[1].x)

for i=2:5
  s.x=i
  push!(data,s)
  println(data[i].x)
end

```

Run it in Julia:

```julia
julia> include("test2.jl")
1
2
3
4
5

```

So it seems to work.  
But if i now enter `data[2].x` (or `data[1].x`, `data[3].x`…) in the command line:

```julia
julia> data[2].x
5

```

or another example:

```julia
julia> data
5-element Array{State,1}:
 State(5)
 State(5)
 State(5)
 State(5)
 State(5)

```

I don’t understand why data is filled correctly during the for-loop but afterwards the data Arrays only contains the last `State` with `x=5`  
I would appreciate any help. thanks in advance

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 9, 2016, 8:59pm UTC](https://discourse.julialang.org/t/need-help-with-adding-composite-types-to-arrays/838/2 "2016-12-09T20:59:30Z")

</div>

You kept mutating and adding the same type. If you want different instances of `State`, you need to either construct different instances of state like

```julia
for i=2:5
  s = State(i)
  push!(data,s)
  println(data[i].x)
end

```

or copy and mutate

```julia
for i=2:5
  s_tmp = copy(s) # might need to deepcopy depending on what the type holds
  s_tmp.x=i
  push!(data,s_tmp)
  println(data[i].x)
end

```

Yes, these will allocate but you have to allocate if you want a different object (no magic will get around that limitation)!

---

<div class="post-metadata">

**Author:** ![Aekiman](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Aekiman](https://discourse.julialang.org/u/Aekiman)\
**Post date:** [December 9, 2016, 9:15pm UTC](https://discourse.julialang.org/t/need-help-with-adding-composite-types-to-arrays/838/3 "2016-12-09T21:15:00Z")

</div>

ah okay. I thought i could change `State` by just using `s.x=i` and add it to the array as a new `State`  
I tried your first suggestion and it’s running perfectly now.  
Thank you very much for your quick help!

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [December 10, 2016, 1:04am UTC](https://discourse.julialang.org/t/need-help-with-adding-composite-types-to-arrays/838/4 "2016-12-10T01:04:02Z")

</div>

Even though you didn’t ask, here is a further bit of advice. When building an array of composite objects in which each object is a relatively small ‘bits’ type item, an `immutable` rather than a `type` is usually preferred for the objects. The immutable would typically give better performance because there is one fewer level of indirection. Furthermore, there is no possibility that two array entries with different indices would actually be the same object (as in your original code), thus making it simpler to reason about code correctness.

---

<div class="post-metadata">

**Author:** ![Aekiman](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Aekiman](https://discourse.julialang.org/u/Aekiman)\
**Post date:** [December 10, 2016, 8:43am UTC](https://discourse.julialang.org/t/need-help-with-adding-composite-types-to-arrays/838/5 "2016-12-10T08:43:22Z")

</div>

Thanks for your advice. I will try using `immutable` instead of `type`.
