Forums Register Login

Array Index Starts from Zero

+Pie Number of slices to send: Send
why array index starts from Zero ?
why not from One or any +ve or -ve number as suitable
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
It means you can reliably find the middle element with n / 2 where n is the length of the array.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Even with an even‑length array, n / 2 gets you as near as possible to its middle.
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 980 times.
Similar Threads
where array index starts from 0
Why array index starts with Zero 0?
Array index start with zero?
Out of bounds Exception
Calendar Class (Wrong month! )
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:53:04.