# When I declare an empty array, what does the output given by the typeof method mean?

**URL:** https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546
**Category:** New to Julia
**Created:** [September 30, 2020, 4:53pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546 "2020-09-30T16:53:10Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)
#### Post date: [September 30, 2020, 4:53pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/1 "2020-09-30T16:53:10Z")

</div>

When I declare an empty array and call `typeof()`, I get the following output:

```julia
numbers = []
println(typeof(numbers))

# Output:
Array{Any,1}

```

1. What does the `1` mean in this case? Does it mean it’s a one dimensional array?
2. Does the documentation present a clear way to declare an array?

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [September 30, 2020, 4:59pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/2 "2020-09-30T16:59:27Z")

</div>

Yes, the 1 means that it’s a one-dimensional array. In general the types of arrays are of the form `SomeArray{T,N}`, where `T` is the element type and `N` is the number of dimensions.

You may define arrays, for example, as

```julia
julia> Array{Int,2}(undef, 2, 2)
2×2 Array{Int64,2}:
 0 0
 0 0

```

where the `undef` specifies that the elements are undeclared, and the other arguments specify the sizes along each dimension.

---

<div class="post-metadata">

### Author: ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)
#### Post date: [September 30, 2020, 5:07pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/3 "2020-09-30T17:07:07Z")

</div>

If I wanted to define an empty array of `Ints` with zero elements, I can do this:

```julia
numbers = Array{Int,1}(undef, 0)

```

Is the initialization correct for an integer array with 0 elements?

and if I wanted to add elements I can do:

```julia
push!(numbers, 10,1,7,3)

```

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [September 30, 2020, 5:07pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/4 "2020-09-30T17:07:15Z")

</div>

You can also create an empty array like this:

```julia
a = Int[]

```

This one is typed to `Int`. As opposed to `Any`.

---

<div class="post-metadata">

### Author: ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)
#### Post date: [September 30, 2020, 5:12pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/5 "2020-09-30T17:12:59Z")

</div>

Thanks for the quick reply.

I forgot to hit reply before I posted my followup question, see below. It’s the one about declaring an empty one dimensional array of `Ints`

---

<div class="post-metadata">

### Author: ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)
#### Post date: [September 30, 2020, 5:19pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/6 "2020-09-30T17:19:23Z")

</div>

Is doing this also an acceptable way of declaring a `0` element, 1 dimensional array of `Ints`:

```julia
numbers = Array{Int,1}(undef, 0)

```

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [September 30, 2020, 5:28pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/7 "2020-09-30T17:28:48Z")

</div>

Yes, or you can even leave off the `1` since it is implicit from the size:

```julia
julia> numbers = Array{Int,1}(undef, 0)
Int64[]

julia> numbers = Array{Int}(undef, 0)
Int64[]

```

---

<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: [September 30, 2020, 5:41pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/8 "2020-09-30T17:41:12Z")

</div>

You can also use the alias `Vector`:

```julia
numbers = Vector{Int}() 

```

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [September 30, 2020, 6:44pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/9 "2020-09-30T18:44:39Z")

</div>

My favorite: `getindex(Int)`.

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [September 30, 2020, 7:51pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/10 "2020-09-30T19:51:30Z")

</div>

There are also `zeros` and `fill` functions to generate arrays, as well as comprehensions and generators.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [September 30, 2020, 8:19pm UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/11 "2020-09-30T20:19:47Z")

</div>

> [@fredrikekre](#):
>
> My favorite: `getindex(Int)` .

I was really confused as why this would work, until I looked up the documentation:

```julia
  getindex(type[, elements...])

  Construct a 1-d array of the specified type. This is usually called with the
  syntax Type[]. Element values can be specified using Type[a,b,c,...].

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> Int8[1, 2, 3]
  3-element Array{Int8,1}:
   1
   2
   3
  
  julia> getindex(Int8, 1, 2, 3)
  3-element Array{Int8,1}:
   1
   2
   3

```

---

<div class="post-metadata">

### Author: ![ZhiyuanYao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhiyuanyao/32/15488_2.png) [@ZhiyuanYao](https://discourse.julialang.org/u/ZhiyuanYao)
#### Post date: [May 3, 2021, 2:32am UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/12 "2021-05-03T02:32:58Z")

</div>

This method is not mentioned in the official documentation. Is it guaranteed?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [May 3, 2021, 2:48am UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/13 "2021-05-03T02:48:57Z")

</div>

the doc mentions `Vector{T}` is just an alias:  
[https://docs.julialang.org/en/v1/base/arrays/#Base.Vector](https://docs.julialang.org/en/v1/base/arrays/#Base.Vector)

---

<div class="post-metadata">

### Author: ![ZhiyuanYao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhiyuanyao/32/15488_2.png) [@ZhiyuanYao](https://discourse.julialang.org/u/ZhiyuanYao)
#### Post date: [May 3, 2021, 2:50am UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/14 "2021-05-03T02:50:40Z")

</div>

Still the method `Array{T,1}()` is not documented. Or am I mising something?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [May 3, 2021, 3:05am UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/15 "2021-05-03T03:05:28Z")

</div>

~~not sure what do you mean~~ , it’s documented here:  
[https://docs.julialang.org/en/v1/base/arrays/#Core.Array-Tuple{UndefInitializer,%20Any}](https://docs.julialang.org/en/v1/base/arrays/#Core.Array-Tuple%7BUndefInitializer,%20Any%7D)

maybe you mean that `Array{T, 1}()` is the same as `Array{T,1}(undef, 0)` is not spelled out in the docs?

---

<div class="post-metadata">

### Author: ![ZhiyuanYao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhiyuanyao/32/15488_2.png) [@ZhiyuanYao](https://discourse.julialang.org/u/ZhiyuanYao)
#### Post date: [May 3, 2021, 3:14am UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/16 "2021-05-03T03:14:40Z")

</div>

Yes, exactly. I have not see a place where `Array{T, 1}()` pops out, only constructors such as `Array{T}(undef, dims)`.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [May 3, 2021, 3:16am UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/17 "2021-05-03T03:16:01Z")

</div>

According to the documentation, the fact that `Array{T, 1}()` works is an additional method to the constructor: [Constructors · The Julia Language](https://docs.julialang.org/en/v1/manual/constructors/#man-outer-constructor-methods)  
So, I agree that its use should be documented.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [May 3, 2021, 3:17am UTC](https://discourse.julialang.org/t/when-i-declare-an-empty-array-what-does-the-output-given-by-the-typeof-method-mean/47546/18 "2021-05-03T03:17:49Z")

</div>

my personal take is that an empty constructor either doesn’t work (throw an error), or does something. The “something” has a pretty natural choice here for dense Array so idk if we need to give it some special place. But then again, adding a docstring costs nothing 🤷‍♀️ .

btw, `T[]` is mentioned in the Array section of the docs/Manual. It’s both shorter and clearer than `Vector{T}()`
