# A question about code

**URL:** https://discourse.julialang.org/t/a-question-about-code/37694
**Category:** New to Julia
**Tags:** question
**Created:** [April 16, 2020, 11:43am UTC](https://discourse.julialang.org/t/a-question-about-code/37694 "2020-04-16T11:43:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)
#### Post date: [April 16, 2020, 11:43am UTC](https://discourse.julialang.org/t/a-question-about-code/37694/1 "2020-04-16T11:43:20Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/e/a/ea8925819d082f2ba8b8457e8baa9b487bca9914.png)

```julia
A=Any[]
for i=1:3
    A[i]=2
    #print(A)
end

```

what is the matter about the code?thank you!

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [April 16, 2020, 11:54am UTC](https://discourse.julialang.org/t/a-question-about-code/37694/2 "2020-04-16T11:54:08Z")

</div>

As it is written in error, your array `A` is empty, you can easily check it with

```julia
length(A) # 0

```

As such you can’t assign value to an element which is outside of the bounds of the array.

You can do one of the following:

1. Preallocate array if you know it’s length beforehand

```julia
A = Vector{Any}(undef, 3)
 for i=1:3
    A[i]=2
    #print(A)
end

```

2.If you do not know size of the array beforehand, you can add new elements (and effectively change it size) with the function `push!`

```julia
A = []
for i in 1:3
  push!(A, 2)
end

```

More details can be found here: [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Basic-Functions-1) and here: [Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/)

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [April 16, 2020, 11:57am UTC](https://discourse.julialang.org/t/a-question-about-code/37694/3 "2020-04-16T11:57:13Z")

</div>

I’d like to add that, if possible, try and make the array concretely typed. E.g.,

```julia
A = Vector{Float64}(undef, 3)
A = Vector{Int}(undef, 3)
A = Float64[]
A = Int[]

```

This matters for performance, not correctness, so it’s less important, but nice to keep in mind.

---

<div class="post-metadata">

### Author: ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)
#### Post date: [April 16, 2020, 12:05pm UTC](https://discourse.julialang.org/t/a-question-about-code/37694/4 "2020-04-16T12:05:34Z")

</div>

thank you !

---

<div class="post-metadata">

### Author: ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)
#### Post date: [April 16, 2020, 12:12pm UTC](https://discourse.julialang.org/t/a-question-about-code/37694/5 "2020-04-16T12:12:30Z")

</div>

it works,thank you!
