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

**URL:** <https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960>\
**Category:** New to Julia\
**Created:** [October 8, 2020, 1:28am UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960 "2020-10-08T01:28:04Z")\
**Posts on this page:** 10\
**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:** [October 8, 2020, 1:28am UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/1 "2020-10-08T01:28:04Z")

</div>

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

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

```

but when I declare an array like this:

```julia
b = Array{Int32}(undef, 0)

```

Can I add elements during initialization?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [October 8, 2020, 1:48am UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/2 "2020-10-08T01:48:40Z")

</div>

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:

```julia
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

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

```

---

<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:** [October 8, 2020, 1:55am UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/3 "2020-10-08T01:55:10Z")

</div>

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

```julia
 b = Array{Int32}(undef, 0)

```

is because it’s mentioned in the [documentation](https://docs.julialang.org/en/v1/base/arrays/#Core.Array-Tuple%7BUndefInitializer,Any%7D). 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:

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

```

the only way to add values during initialization?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 8, 2020, 2:18am UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/4 "2020-10-08T02:18:26Z")

</div>

> [@anon60034542](#):
>
> I was wondering if you can insert values when using this specific method for creating an array.

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.

---

<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:** [October 8, 2020, 2:25am UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/5 "2020-10-08T02:25:19Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 8, 2020, 2:28am UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/6 "2020-10-08T02:28:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [October 8, 2020, 2:35am UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/7 "2020-10-08T02:35:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 8, 2020, 3:33am UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/8 "2020-10-08T03:33:47Z")

</div>

> [@tbeason](#):
>
> If you want to initialize with values

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.

---

<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:** [October 8, 2020, 2:27pm UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/9 "2020-10-08T14:27:37Z")

</div>

> [@yuyichao](#):
>
> Now if you are asking about other ways to initialize array with values provided in different ways (there are many).

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.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 8, 2020, 2:48pm UTC](https://discourse.julialang.org/t/how-to-add-elements-to-an-array-when-its-declared-like-this/47960/10 "2020-10-08T14:48:21Z")

</div>

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.
