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

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

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?
1 Like

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> 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.

7 Likes

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

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:

push!(numbers, 10,1,7,3)
2 Likes

You can also create an empty array like this:

a = Int[]

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

8 Likes

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

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

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

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

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

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

You can also use the alias Vector:

numbers = Vector{Int}() 
7 Likes

My favorite: getindex(Int).

2 Likes

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

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

  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
8 Likes

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

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

1 Like

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

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

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

1 Like

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

According to the documentation, the fact that Array{T, 1}() works is an additional method to the constructor: Constructors · The Julia Language
So, I agree that its use should be documented.

2 Likes

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 :woman_shrugging: .

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

2 Likes