72

There are two things about arrays that sometimes confuse me:

[0]: They start at zero
[1]: They end at one less than the length

Comments
  • 0
    I originally read two as three... And it sorta still made sense
  • 15
    Technically, second rule is not a rule is a direct consequences of the 1st rule. You know the rule number 0.
  • 2
    @thedev Yeah but it's appropriate because 1 (The index of the second item) is one less than the length of the list.
  • 2
    @AlgoRythm the index of the first item is already one less than the length. Because it starts at 0.
  • 1
    They end at one less than the length because they start at zero
  • 2
    Easy way to remember
    index = position
    length = number of elements
  • 0
  • 6
    Except Python arrays, whose last element is at -1. Oh I so envy them.
  • 0
  • 0
    You could use something like Lua. Granted, you then have the problem that you're using Lua, but you'll like the arrays!
  • 0
    The Element of Index 0 ends where 1 begins, so the length thing makes sense. If there is 1 element that starts @0 (ends @1) then then len is 1.
  • 0
    Why does it confuse you? If you know it starts at index 0 then the end must be length - 1.
  • 1
    @simeg It's mostly for the joke man, but sometimes it is a little confusing when applying it to data structures and not counting the normal way.
  • 1
    But they don't start at 0. They just index starting from zero. It can sometimes get confusing tho, whether you should add 1, subtract 1, just leave it, etc.
Add Comment