# BoundsError: attempt to access 0-element Array{Any,1} at index \[1\]

**URL:** https://discourse.julialang.org/t/boundserror-attempt-to-access-0-element-array-any-1-at-index-1/65360
**Category:** General Usage
**Created:** [July 27, 2021, 9:21am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-0-element-array-any-1-at-index-1/65360 "2021-07-27T09:21:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kip0d](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kip0d](https://discourse.julialang.org/u/kip0d)
#### Post date: [July 27, 2021, 9:21am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-0-element-array-any-1-at-index-1/65360/1 "2021-07-27T09:21:48Z")

</div>

Hello, this is my code:

```julia
distvec = []
for indm = 1:M
    global distvec
    distvec[indm] = -(-Nvec[1]* px[indm]- Nvec[2]* py[indm]- Nvec[3]* pz[indm])/sqrt(Nvec[1]^2+Nvec[2]^2+Nvec[3]^2) 
end

```

The error I get:

```julia
BoundsError: attempt to access 0-element Array{Any,1} at index [1]

```

How do I solve this issue?

Thank you!

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 27, 2021, 9:49am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-0-element-array-any-1-at-index-1/65360/2 "2021-07-27T09:49:58Z")

</div>

You want to preallocate `distvec` to the correct length, e.g. if your numbers are floats do

```julia
distvec = zeros(M)

```

or `Array{Float64, 1}(undef, M)` if you want to leave the elements uninitialized.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [July 27, 2021, 9:51am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-0-element-array-any-1-at-index-1/65360/3 "2021-07-27T09:51:24Z")

</div>

The line `distvec = []` declares an empty array, so you cannot do `distvec[1] = 0.1`. You could do `push!(distvec, 0.1)`, but it’s better to pre-allocate the array with the correct size.

Also, avoid globals as much as possible. If you put this code in a function you don’t need a global:

```julia
function f(M, Nvec, px, py, pz)
    # Preallocate an array full of 0.0 (write e.g. zeros(Int, M) to choose a type different from Float64)
    distvec = zeros(M)
    for indm = 1:M
        distvec[indm] = -(-Nvec[1]* px[indm]- Nvec[2]* py[indm]- Nvec[3]* pz[indm])/sqrt(Nvec[1]^2+Nvec[2]^2+Nvec[3]^2) 
    end
    return distvec
end

f(5, [1,2,3], 1:5, 1:5, 1:5)

```

If you don’t want to preallocate, at least create an array with a specific type, e.g. `distvec = Float64[]` instead of `distvec = []`.

You can also use array comprehension:

```julia
M = 5
Nvec = [1,2,3]
px = py = pz = 1:M

distvec = [-(-Nvec[1]*px[indm] - Nvec[2]*py[indm] - Nvec[3]*pz[indm]) / norm(Nvec)
           for indm in 1:M]

```

---

<div class="post-metadata">

### Author: ![kip0d](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kip0d](https://discourse.julialang.org/u/kip0d)
#### Post date: [July 27, 2021, 10:12am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-0-element-array-any-1-at-index-1/65360/4 "2021-07-27T10:12:09Z")

</div>

Thank you all!
