# How to declare an 1-dim Array of 310 1-dimensional Arrays with Int32 elements

**URL:** <https://discourse.julialang.org/t/how-to-declare-an-1-dim-array-of-310-1-dimensional-arrays-with-int32-elements/50119>\
**Category:** New to Julia\
**Created:** [November 14, 2020, 12:01am UTC](https://discourse.julialang.org/t/how-to-declare-an-1-dim-array-of-310-1-dimensional-arrays-with-int32-elements/50119 "2020-11-14T00:01:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reb](https://avatars.discourse-cdn.com/v4/letter/r/51bf81/32.png) [@reb](https://discourse.julialang.org/u/reb)\
**Post date:** [November 14, 2020, 12:01am UTC](https://discourse.julialang.org/t/how-to-declare-an-1-dim-array-of-310-1-dimensional-arrays-with-int32-elements/50119/1 "2020-11-14T00:01:38Z")

</div>

I’m trying to declare a 1-dimensional Array of 310 1-dimensional  
arrays with an unspecified number of Int32 elements.

I thought Array{Array{Int32}}(310) was correct (at least circa 2017),  
but it fails.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 14, 2020, 12:48am UTC](https://discourse.julialang.org/t/how-to-declare-an-1-dim-array-of-310-1-dimensional-arrays-with-int32-elements/50119/2 "2020-11-14T00:48:26Z")

</div>

You have to use `undef` now:

```julia
 Array{Array{Int32}}(undef, 310) 

```

Edit: Also you should use `Vector` instead of `Array`. Array{T} is still abstract because it doesn’t specify the dimension. `Vector{T}` is an alias for `Array{T, 1}`

---

<div class="post-metadata">

**Author:** ![reb](https://avatars.discourse-cdn.com/v4/letter/r/51bf81/32.png) [@reb](https://discourse.julialang.org/u/reb)\
**Post date:** [November 20, 2020, 11:45pm UTC](https://discourse.julialang.org/t/how-to-declare-an-1-dim-array-of-310-1-dimensional-arrays-with-int32-elements/50119/3 "2020-11-20T23:45:35Z")

</div>

Tom,

Thanks for the ‘undef’ info–I doubt that I would ever have found that on my own.

I also saw your  
Edit: Also you should use Vector instead of Array. Array{T} is still abstract because it doesn’t specify the dimension. Vector{T} is an alias for Array{T, 1}

While the declaration seems fine–no errors when executing it, everything I try to do after that fails:

If I define P=Vector{Vector{Int32}}(undef,310), how do I refer to each of the 310 vectors?  
I would have thought P[1] would be the first of the 310 vectors, but apparently not.

I need to initialize the first two elements of each of the 310 vectors to zero. I would have thought  
that P[1][1] and P[1][2] would be the first two elements of the first vector, but apparently not.  
I can’t set them to zero.

After I get the first two elements of each of the 310 vectors defined to be zero, I plan to define subsequent  
elements via push!..but it looks like that won’t work either:

# VectorVector.jl Trying to make push! work with a vector of vectors.

P = Vector{Vector{Int32}}(undef,310)  
push!(P[1],1999) # Pushes the integer 1999 into the first open position of vector ‘P[1]’, i.e. P[1][1].  
push!(P[1],2020) # Pushes the integer 2020 into the first open position of vector ‘P[1]’, i.e. P[1][2].  
println(‘P[1][1]=’,P[1][1])  
println(‘P[1][2]=’,P[1[[2])

c:\>j vectorvector  
ERROR: LoadError: UndefRefError: access to undefined reference  
Stacktrace:  
[1] getindex(::Array{Array{Int32,1},1}, ::Int64) at .\array.jl:809  
[2] top-level scope at j:vectorvector.jl:4  
[3] include(::Function, ::Module, ::String) at .\Base.jl:380  
[4] include(::Module, ::String) at .\Base.jl:368  
[5] exec\_options(::Base.JLOptions) at .\client.jl:296  
[6] \_start() at .\client.jl:506  
in expression starting at j:vectorvector.jl:4

c:\>

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 21, 2020, 12:01am UTC](https://discourse.julialang.org/t/how-to-declare-an-1-dim-array-of-310-1-dimensional-arrays-with-int32-elements/50119/4 "2020-11-21T00:01:40Z")

</div>

Are you working in the REPL? If not, I highly suggest centering your workflow around it.

Maybe this can shed some light on what’s going on:

```julia
julia> P = Vector{Vector{Int32}}(undef, 3)
3-element Array{Array{Int32,1},1}:
 #undef
 #undef
 #undef

julia> P[1]
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(::Array{Array{Int32,1},1}, ::Int64) at ./array.jl:809
 [2] top-level scope at REPL[181]:1

julia> P[1] = [];

julia> P
3-element Array{Array{Int32,1},1}:
    []
 #undef
 #undef

```

The array initially contains undefined references (that’s what undef does). If you want to make an array of empty arrays, you can use a comprehension like

```julia
[Int32[] for i in 1:310]

```

Feel free to ask for any further clarification.
