# Add instantiation of object to vector

**URL:** https://discourse.julialang.org/t/add-instantiation-of-object-to-vector/42867
**Category:** New to Julia
**Tags:** vector, struct
**Created:** [July 10, 2020, 7:39pm UTC](https://discourse.julialang.org/t/add-instantiation-of-object-to-vector/42867 "2020-07-10T19:39:43Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![zazizoma](https://avatars.discourse-cdn.com/v4/letter/z/5f8ce5/32.png) [@zazizoma](https://discourse.julialang.org/u/zazizoma)
#### Post date: [July 10, 2020, 7:39pm UTC](https://discourse.julialang.org/t/add-instantiation-of-object-to-vector/42867/1 "2020-07-10T19:39:43Z")

</div>

Hello, I have defined an object via a mutable struct and I would like to instantiate n of them into a vector. Ultimately, I’d like to refer two them as vector[i].

Here’s MWE:

> mutable struct Foo  
> x :: Float64  
> y :: Float64  
> end
> 
> foos = Vector{Foo}
> 
> n = 3
> 
> for i in 1:3  
> foo = Foo(randn(), 0)  
> push!(foos, foo)  
> end

I’d like to reference x in the second foo instance as

> foos[2].x

Thanks for looking!

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 10, 2020, 8:03pm UTC](https://discourse.julialang.org/t/add-instantiation-of-object-to-vector/42867/3 "2020-07-10T20:03:50Z")

</div>

> [@zazizoma](#):
>
> foos = Vector{Foo}

You’ve defined `foos` as the _type_, not as an instance. You just need `Vector{Foo}()` instead:

```julia
julia> foos = Vector{Foo}()
0-element Array{Foo,1}

julia> for i in 1:3
       foo = Foo(randn(), 0)
       push!(foos, foo)
       end

julia> foos[2].x
0.7274441671361673

```

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [July 10, 2020, 11:09pm UTC](https://discourse.julialang.org/t/add-instantiation-of-object-to-vector/42867/4 "2020-07-10T23:09:22Z")

</div>

A simple syntax for an empty array of `Foo` is

`Foo[]`.
