# Initialize array of arrays

**URL:** <https://discourse.julialang.org/t/initialize-array-of-arrays/11610>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 12, 2018, 2:44pm UTC](https://discourse.julialang.org/t/initialize-array-of-arrays/11610 "2018-06-12T14:44:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgbarletta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgbarletta/32/3204_2.png) [@pgbarletta](https://discourse.julialang.org/u/pgbarletta)\
**Post date:** [June 12, 2018, 2:44pm UTC](https://discourse.julialang.org/t/initialize-array-of-arrays/11610/1 "2018-06-12T14:44:00Z")

</div>

Hi!  
I’m creating a matrix of arrays:

```julia
hist = Array{Array{Int64, 1}, 2}(x, y)

```

and filling some of its elements, but others will be left uninitialized.  
I was wondering if there is some syntax available to default-initialize all the `Array{Int64, 1}` elements, so they do not show as `#undef`.  
I could iterate over all elements, checking for those that were left uninitialized `isdefined(hist, i*j)` and then initializing them to empty arrays `hist[i, j] = Array{Int64, 1}()`, but I was wondering if there is a better way of doing this.

Basically, I’m trying to replicate C++ RAII idiom, so I don’t get `UndefError`s

Thanks!

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 12, 2018, 2:53pm UTC](https://discourse.julialang.org/t/initialize-array-of-arrays/11610/2 "2018-06-12T14:53:45Z")

</div>

You can do

```julia
julia> fill(Int[], 2, 3)
2×3 Array{Array{Int64,1},2}:
 [] [] []
 [] [] []

```

if it is fine to have the same empty vector everywhere or

```julia
julia> [Int[] for i=1:2, j=1:3]
2×3 Array{Array{Int64,1},2}:
 [] [] []
 [] [] []

```

if you need distinct empty vectors. The difference mostly matters if you intend to push data into the elements rather than replace them with new vectors.

---

<div class="post-metadata">

**Author:** ![pgbarletta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgbarletta/32/3204_2.png) [@pgbarletta](https://discourse.julialang.org/u/pgbarletta)\
**Post date:** [June 12, 2018, 3:26pm UTC](https://discourse.julialang.org/t/initialize-array-of-arrays/11610/3 "2018-06-12T15:26:18Z")

</div>

Ooohhh, right `fill()` can be used to construct&initialize arrays.  
Thanks! This was exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 12, 2018, 3:44pm UTC](https://discourse.julialang.org/t/initialize-array-of-arrays/11610/4 "2018-06-12T15:44:55Z")

</div>

Note, as said above, that `fill` will initialize the array of arrays with the **same** array:

```julia
julia> a = fill(Int[],2,2)
2×2 Array{Array{Int64,1},2}:
 Int64[] Int64[]
 Int64[] Int64[]

julia> push!(a[1,1],1)
1-element Array{Int64,1}:
 1

julia> a
2×2 Array{Array{Int64,1},2}:
 [1] [1]
 [1] [1]

```
