# How to initialize two dimensional array of arrays?

**URL:** <https://discourse.julialang.org/t/how-to-initialize-two-dimensional-array-of-arrays/26635>\
**Category:** New to Julia\
**Created:** [July 22, 2019, 3:22pm UTC](https://discourse.julialang.org/t/how-to-initialize-two-dimensional-array-of-arrays/26635 "2019-07-22T15:22:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [July 22, 2019, 3:22pm UTC](https://discourse.julialang.org/t/how-to-initialize-two-dimensional-array-of-arrays/26635/1 "2019-07-22T15:22:29Z")

</div>

Dear community,

I fail to initialize an array of type `Array{Array{UInt16,1},2}`. In particular I tried it via a mutable struct, which in my case then leads to type `Array{Array{UInt16,N} where N,2}` which I can use as wanted. However, it seems to me that there is a conversion hidden behind that, since accessing elements allocates memory. Here is a small example:

```julia
using BenchmarkTools

mutable struct myinst
    R::Array{Array{UInt16},2}
end

function main()
    N = 1000
    W = 500
    K = 1000

    inst = myinst( Array{Array{UInt16}}(undef,0,0) )
    inst.R = [Array{UInt16}[] for i=1:N, j=1:W]

    S = Array{Array{UInt16}}(undef,0,0)
    S = [Array{UInt16}[] for i=1:N, j=1:W]

    println( "typeof inst.R: ", typeof(inst.R) )
    println( "typeof S: ", typeof(S) )
    return nothing
end
main()

```

which outputs:

```julia
typeof inst.R: Array{Array{UInt16,N} where N,2}
typeof S: Array{Array{Array{UInt16,N} where N,1},2}

```

I then fill `inst.R[i,j]` with arrays of different sizes. However, reading the data afterwards allocates memory. Can you please tell me how to initialize such a structure correctly? Is the memory allocation when reading the data really due to some conversion in the background?  
Thank you in advance, Michael.

---

<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 22, 2019, 3:36pm UTC](https://discourse.julialang.org/t/how-to-initialize-two-dimensional-array-of-arrays/26635/2 "2019-07-22T15:36:09Z")

</div>

Your field type is `Array{Array{UInt16}, 2}`, which isn’t quite right. That type signature means “a two-dimensional array, where each element is (an array of `UInt16` of _any dimension_)”.

What is the dimensionality of each element of `R`? If R is a 2D array of 1D arrays of UInt16, then you want:

```julia
Array{Array{UInt16, 1}, 2}

```

or, equivalently:

```julia
Matrix{Vector{UInt16}}

```

Or if each element of `R` is a 2D array of `UInt16`, then you want:

```julia
Array{Array{UInt16, 2}, 2}

```

which is exactly the same as `Matrix{Matrix{UInt16}}`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 22, 2019, 4:13pm UTC](https://discourse.julialang.org/t/how-to-initialize-two-dimensional-array-of-arrays/26635/3 "2019-07-22T16:13:15Z")

</div>

You should really use the `Vector` and `Matrix` aliases. It is _extremely_ easy to make a mistake otherwise.

It also makes your code much more readable.

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [July 22, 2019, 4:15pm UTC](https://discourse.julialang.org/t/how-to-initialize-two-dimensional-array-of-arrays/26635/4 "2019-07-22T16:15:36Z")

</div>

Wow, that easy. Thank you @rdeits it works, thank you @DNF I will consider that.

However, what I still do not understand. Even if the inner array is of _any dimension_ as I initially defined it, is this the reason that reading data from it allocates memory? At least I cannot imagine why this should be the case.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 22, 2019, 4:23pm UTC](https://discourse.julialang.org/t/how-to-initialize-two-dimensional-array-of-arrays/26635/5 "2019-07-22T16:23:58Z")

</div>

This is typical when you’re working with data that is of an abstract type, the compiler is unable to specialize the code. It’s fine to use abstract types for dispatch, but your data should preferably have a concrete type.

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [July 22, 2019, 4:25pm UTC](https://discourse.julialang.org/t/how-to-initialize-two-dimensional-array-of-arrays/26635/6 "2019-07-22T16:25:44Z")

</div>

Okay, this makes sense to me. Thank you so much!
