# Is it possible to perform push!/pop!/deleteat! on an OffsetArray?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-perform-push-pop-deleteat-on-an-offsetarray/17787>\
**Category:** General Usage\
**Created:** [November 20, 2018, 6:46pm UTC](https://discourse.julialang.org/t/is-it-possible-to-perform-push-pop-deleteat-on-an-offsetarray/17787 "2018-11-20T18:46:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SZJX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/szjx/32/4036_2.png) [@SZJX](https://discourse.julialang.org/u/SZJX)\
**Post date:** [November 20, 2018, 6:46pm UTC](https://discourse.julialang.org/t/is-it-possible-to-perform-push-pop-deleteat-on-an-offsetarray/17787/1 "2018-11-20T18:46:44Z")

</div>

Maybe I haven’t understood the usage of `OffsetArray` fully. I am porting some code from C++ and would like to have an 0-based indexing array on which one can still perform actions such as `push!` or `delete!` (i.e. the length is not fixed). Is this possible to achieve with `OffsetArrays.jl` or any other existing package? Or should I consider writing my own type extending `AbstractArray`.

The algorithm (which involves the depth of a tree) will be all over the place without 0-based indexing, which is quite annoying. Basically what I want is an array similar to the default array, with the only difference being that its index starts from 0.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 20, 2018, 7:34pm UTC](https://discourse.julialang.org/t/is-it-possible-to-perform-push-pop-deleteat-on-an-offsetarray/17787/2 "2018-11-20T19:34:21Z")

</div>

It’s not an operation that has a method defined, but you can do it manually:

```julia
julia> using OffsetArrays

julia> a = OffsetArray(collect(1:5), (-1,))
OffsetArray(::Array{Int64,1}, 0:4) with eltype Int64 with indices 0:4:
 1
 2
 3
 4
 5

julia> push!(a, 6)
ERROR: MethodError: no method matching push!(::OffsetArray{Int64,1,Array{Int64,1}}, ::Int64)
Closest candidates are:
  push!(::Any, ::Any, ::Any) at abstractarray.jl:2064
  push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:2065
  push!(::Array{Any,1}, ::Any) at array.jl:862
  ...
Stacktrace:
 [1] top-level scope at none:0

julia> push!(parent(a), 6)
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

julia> a
OffsetArray(::Array{Int64,1}, 0:5) with eltype Int64 with indices 0:5:
 1
 2
 3
 4
 5
 6

```

(A pull request to implement `push!` directly on an `OffsetVector{T,N,<:Vector}` would be merged if submitted.)
