# Predefined array too large

**URL:** https://discourse.julialang.org/t/predefined-array-too-large/29623
**Category:** New to Julia
**Created:** [October 7, 2019, 10:10pm UTC](https://discourse.julialang.org/t/predefined-array-too-large/29623 "2019-10-07T22:10:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)
#### Post date: [October 7, 2019, 10:10pm UTC](https://discourse.julialang.org/t/predefined-array-too-large/29623/1 "2019-10-07T22:10:25Z")

</div>

I predefined a vector E to be the same size as vector D.

```julia
E = typeof(D)(undef, length(D))

```

In most cases, I expect E to have the same number of elements as D, but I still want to be able to handle the case where E has fewer elements than D. In those cases, this declaration produces a vector with extra undefined elements that I would like to trim off.

For example, it might show

```julia
E = Array{Rational{Int128},1}[[1//1, 0//1, 0//1], [0//1, 1//1, 0//1], [0//1, 0//1, 1//1], #undef, #undef, #undef]

```

I tried to use a while loop to `pop!` of the end elements

```julia
while length(E) > numInE     
     pop!(E)
end

```

but this returned an `UndefRefError: access to undefined reference`.

Is there a way I can trim the end of an array efficiently?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 7, 2019, 10:17pm UTC](https://discourse.julialang.org/t/predefined-array-too-large/29623/2 "2019-10-07T22:17:24Z")

</div>

You can use `resize!`. Also, instead of the `typeof` thing, look into the `similar` function.

---

<div class="post-metadata">

### Author: ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)
#### Post date: [October 7, 2019, 10:17pm UTC](https://discourse.julialang.org/t/predefined-array-too-large/29623/3 "2019-10-07T22:17:49Z")

</div>

Thanks! Will do 🙂
