# How to access the end index of an array as attribute of a class

**URL:** <https://discourse.julialang.org/t/how-to-access-the-end-index-of-an-array-as-attribute-of-a-class/38070>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 23, 2020, 7:49am UTC](https://discourse.julialang.org/t/how-to-access-the-end-index-of-an-array-as-attribute-of-a-class/38070 "2020-04-23T07:49:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 23, 2020, 7:49am UTC](https://discourse.julialang.org/t/how-to-access-the-end-index-of-an-array-as-attribute-of-a-class/38070/1 "2020-04-23T07:49:13Z")

</div>

```julia
struct Test
    data::Vector{Int}
end

Base.getindex(test::Test, index) = test.data[index]

```

Then, to get idex:

```julia
test = Test([1,2,3])

```

I can get:

```julia
test[1]
test[2]

```

How to get the last index value  
`test[end] `didnot work.

Thank you for reading my question

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [April 23, 2020, 7:53am UTC](https://discourse.julialang.org/t/how-to-access-the-end-index-of-an-array-as-attribute-of-a-class/38070/2 "2020-04-23T07:53:04Z")

</div>

This could work:

```julia
 test[:][end]

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [April 23, 2020, 8:02am UTC](https://discourse.julialang.org/t/how-to-access-the-end-index-of-an-array-as-attribute-of-a-class/38070/3 "2020-04-23T08:02:52Z")

</div>

> [@QUANG\_VU\_NGUY\_N\_PH\_M](#):
>
> How to get the last index value  
> `test[end] ` didnot work.

The error message seems clear enough to me, but maybe I’m just used to these things.

```julia
julia> test[end]
ERROR: MethodError: no method matching lastindex(::Test)
Closest candidates are:
  lastindex(::Markdown.MD) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Markdown/src/parse/parse.jl:26
  lastindex(::Base64.Buffer) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Base64/src/buffer.jl:19
  lastindex(::Cmd) at process.jl:639
  ...
Stacktrace:
 [1] top-level scope at REPL[9]:1

help?> lastindex
search: lastindex

  lastindex(collection) -> Integer
  lastindex(collection, d) -> Integer

  Return the last index of collection. If d is given, return the last index of collection along dimension d.

  The syntaxes A[end] and A[end, end] lower to A[lastindex(A)] and A[lastindex(A, 1), lastindex(A, 2)], respectively.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> lastindex([1,2,4])
  3
  
  julia> lastindex(rand(3,4,5), 2)
  4

julia> Base.lastindex(test::Test) = lastindex(test.data)

julia> test[end]
3

```

---

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 23, 2020, 8:21am UTC](https://discourse.julialang.org/t/how-to-access-the-end-index-of-an-array-as-attribute-of-a-class/38070/4 "2020-04-23T08:21:29Z")

</div>

Awesome! Thank you very much for your reply

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [April 23, 2020, 11:56am UTC](https://discourse.julialang.org/t/how-to-access-the-end-index-of-an-array-as-attribute-of-a-class/38070/5 "2020-04-23T11:56:05Z")

</div>

You can mark the previous post as the solution to help others with same question.

---

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 23, 2020, 12:27pm UTC](https://discourse.julialang.org/t/how-to-access-the-end-index-of-an-array-as-attribute-of-a-class/38070/6 "2020-04-23T12:27:59Z")

</div>

Yes, I did. Thank you!
