# Initialization of an empty array, which will be filled in a loop with arrays of different sizes

**URL:** <https://discourse.julialang.org/t/initialization-of-an-empty-array-which-will-be-filled-in-a-loop-with-arrays-of-different-sizes/24463>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 22, 2019, 8:11am UTC](https://discourse.julialang.org/t/initialization-of-an-empty-array-which-will-be-filled-in-a-loop-with-arrays-of-different-sizes/24463 "2019-05-22T08:11:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [May 22, 2019, 8:11am UTC](https://discourse.julialang.org/t/initialization-of-an-empty-array-which-will-be-filled-in-a-loop-with-arrays-of-different-sizes/24463/1 "2019-05-22T08:11:36Z")

</div>

Hello,

I would like to programm an algorithm, where an array will be filled with other arrays of different sizes. Does somebody know how to it?

Best regards

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 22, 2019, 8:15am UTC](https://discourse.julialang.org/t/initialization-of-an-empty-array-which-will-be-filled-in-a-loop-with-arrays-of-different-sizes/24463/2 "2019-05-22T08:15:40Z")

</div>

> [@Volker](#):
>
> Does somebody know how to it?

Probably yes.

But you have to be more specific, at provide at least

1. an example of desired inputs and corresponding outputs,

2. some code that you have tried and need help with.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [May 22, 2019, 8:31am UTC](https://discourse.julialang.org/t/initialization-of-an-empty-array-which-will-be-filled-in-a-loop-with-arrays-of-different-sizes/24463/3 "2019-05-22T08:31:05Z")

</div>

Ok , thanks that makes sense. I didn´t know this. I found a solution, which works for me. I use an array of dicts

```julia
all_soultions = [Dict("solutions" => []) for i in 1:10]
for i in 1:length(all_soultions)
    if i==1
        all_soultions[i]["solutions"] = [1,2,3]
    elseif i ==2
        all_soultions[i]["solutions"] = [1,2]
    elseif i==3
        all_soultions[i]["solutions"] = [1,2,3,4]
    elseif i ==4
        all_soultions[i]["solutions"] = [2]
    end
end

```

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [May 22, 2019, 8:37am UTC](https://discourse.julialang.org/t/initialization-of-an-empty-array-which-will-be-filled-in-a-loop-with-arrays-of-different-sizes/24463/4 "2019-05-22T08:37:30Z")

</div>

What about

```julia
all_soultions = Vector{Vector{Int}}(undef, 10)
for i in 1:length(all_soultions)
    if i==1
        all_soultions[i] = [1,2,3]
    elseif i ==2
        all_soultions[i] = [1,2]
    elseif i==3
        all_soultions[i] = [1,2,3,4]
    elseif i ==4
        all_soultions[i] = [2]
    end
end

```

Or maybe more appropriate depending on your use-case:

```julia
all_soultions = Vector{Int}[]
for i in 1:4
    if i==1
        push!(all_soultions, [1,2,3])
    elseif i ==2
        push!(all_soultions, [1,2])
    elseif i==3
        push!(all_soultions, [1,2,3,4])
    elseif i ==4
        push!(all_soultions, [2])
    end
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 22, 2019, 8:40am UTC](https://discourse.julialang.org/t/initialization-of-an-empty-array-which-will-be-filled-in-a-loop-with-arrays-of-different-sizes/24463/5 "2019-05-22T08:40:52Z")

</div>

> [@Volker](#):
>
> I use an array of dicts

I don’t quite see why you need the `Dict`s here, you are just adding a layer of indirection for no reason apparent in your example.

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [May 22, 2019, 8:48am UTC](https://discourse.julialang.org/t/initialization-of-an-empty-array-which-will-be-filled-in-a-loop-with-arrays-of-different-sizes/24463/6 "2019-05-22T08:48:50Z")

</div>

You are right. I just started to use Julia a few days ago. So I´m a little bit unfamiliar with the syntax and have to learn such things. Thanks both of you for your help.
