Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Index Starts from Zero

 
pratik gaurav
Greenhorn
Posts: 16
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why array index starts from Zero ?
why not from One or any +ve or -ve number as suitable
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a measure of "how far are we from the beginning of the array." It's origins can be traced back to when arrays were simply consecutive chunks of memory, and X[0] meant "what's at location X" and X[N] meant "what's at location X + N, in languages like C and its predecessors. They in turn got it from trying to make friendlier notation that corresponded directly to machine instructions like "fetch from location X + some offset".

And even though Java insulates us from that low-level memory addressing stuff, it still makes some of the math easier, and it makes sense if you view it as "how far are we from the beginning."



If we think of an array as a continuum, with its elements occupying space in that continuum, then in the above array, A occupies the space from 0 (the left edge) to 1 (1 unit away from the left edge), B occupies the space from 1 to 2, and so on. When we're naming the spot occupied by an element, we chose the position at which that element starts.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: And even though Java insulates us from that low-level memory addressing stuff, it still makes some of the math easier, and it makes sense if you view it as "how far are we from the beginning."



If we think of an array as a continuum, with its elements occupying space in that continuum, then in the above array, A occupies the space from 0 (the left edge) to 1 (1 unit away from the left edge), B occupies the space from 1 to 2, and so on. When we're naming the spot occupied by an element, we chose the position at which that element starts.


So, it was a design choice made by people with the foresight that operations on arrays should be as easy as possible mathematically. Correct?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:
So, it was a design choice made by people with the foresight that operations on arrays should be as easy as possible mathematically. Correct?


Design choice, definitely.

As easy as possible mathematically--probably not entirely. I wasn't in the meetings, but I suspect the reason was mainly historical, but with the knowledge of the advantages (and disadvantages) of doing it that way. The advantages being as I already described, and the main disadvantage being that people are used to counting from 1, so it can lead to confusion initially.
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means you can reliably find the middle element with n / 2 where n is the length of the array.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It means you can reliably find the middle element with n / 2 where n is the length of the array.


For arrays with odd length, of course.
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even with an even‑length array, n / 2 gets you as near as possible to its middle.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic