How to add elements to an array when it's declared like this?

As you can see during initialization I can insert numbers between the square brackets.

a = Int32[1,2,3,4,5]

but when I declare an array like this:

b = Array{Int32}(undef, 0)

Can I add elements during initialization?

The definition for b is not quite correct, since you specified an array of dimension 2 (the 2 in {Int32,2}), you need to specify the size of the two dimensions. For example:

b = Array{Int32, 2}(undef, 3, 3)

Creates a 3x3 array of uninitialized values. If you want to just type out a 3x3 array of Int32, you can try

c = Int32[1 2 3; 4 5 6; 7 8 9]
2 Likes

The definition for b is not quite correct, since you specified an array of dimension 2 (the 2 in {Int32,2} ), you need to specify the size of the two dimensions.

Fixed, Thanks!

The only reason why I ask about this particular example:

 b = Array{Int32}(undef, 0)

is because it’s mentioned in the documentation. I was wondering if you can insert values when using this specific method for creating an array.

Of course you can add values after you create it with the push!() method.

In other words, is doing:

c = Int32[1 2 3; 4 5 6; 7 8 9]

the only way to add values during initialization?

Yes you can, there’s no difference.

In almost all cases, the way you created an Array doesn’t matter at all whether you can push to it. The only thing that matters is the dimension. You are allowed to resize a 1-d array but not a higher (or lower) dimensional one, that’s it.

2 Likes

Yes you can, there’s no difference.

Great!

Could you elaborate on how you would add elements during initialization when constructing an array like this:

b = Array{Int32}(undef, 0)

The documentation doesn’t specify how to do this, just how to create an array with a specific type, and dimensions.

Note that you either want b = Array{Int32,1}(undef, 0) or b = Vector{Int32}(undef, 0). Array is an N dimensional array, so you really want to specify N if you use this type of constructor.

I believe the answer to your question is no, you cannot. If you want to initialize with values, you should use brackets and just type it out.

Sorry I misunderstand the initialization part. Well, none of the code here is “declaration”, they are all “definition”. They are just code to create the array so there isn’t much need to give you multiple different ways to create array with initialized values. Now if you are asking about other ways to initialize array with values provided in different ways (there are many) or ways to finish initialization after creating it by calling a constructor (which was how I interpreted the question) than there are many ways to do those.

This! Exactly this! I just wanted to see all the different ways it could be done. All I know of are the two ways shown in my code example above, but only the first example is initialized with values.

1 Like

A few quick ones that comes into mind are,

  1. fill
  2. comprehension
  3. collect
  4. constructor or convert

All of these can return an array that is initialized based on the input that were stored in a different format. There’s little difference in the array they produce so you should not be looking for ways for all different syntax/functions to accept input data in exactly the same format and instead select one that fits you goal, including what input you have, the best.

4 Likes