permaculture playing cards*
The moose likes Beginning Java and the fly likes Array Index Starts from Zero Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Index Starts from Zero" Watch "Array Index Starts from Zero" New topic
Author

Array Index Starts from Zero

pratik gaurav
Greenhorn

Joined: Oct 04, 2012
Posts: 16

why array index starts from Zero ?
why not from One or any +ve or -ve number as suitable
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 38075
    
  22
It means you can reliably find the middle element with n / 2 where n is the length of the array.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 38075
    
  22
Even with an even‑length array, n / 2 gets you as near as possible to its middle.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Index Starts from Zero
 
Similar Threads
Array index start with zero?
Out of bounds Exception
Why array index starts with Zero 0?
where array index starts from 0
Calendar Class (Wrong month! )