Why does an array index start at 0, not 1
Posted by Nick Sieger Thu, 27 Jul 2006 14:57:00 GMT
If you were looking for an answer to this question, you apparently cannot get a straight one in the java forums. Just one of many choice quotes:
Anyway, any logically counting would start at 0.
I tried that once. I referred to my son as “Number Zero Son”. Got a definitely negative response.
Because 0 is considered by most mathematicians to be a real number between -1 and 1 [ref], and so in languages where arrays are positively indexed, zero is the first item (-1 is not possible, the first possible value then is 0).
I can’t say that it is very intuitive or that it matches reality (even though maths is supposed to map to reality): you can’t have zero of a thing, because by definition zero is none of everything.
In the context of comparison it makes sense and matches reality (i.e., you can have “space” for one apple in your basket, but not have an apple in it, and thus have 0 apples in your basket, or 1 space); but as a number like “0 apples, 1 apples, ...” it makes no sense, and thus it has special rules from all other numbers (e.g., it is the only number for which 0 + n = n, and 0 * n = 0 is always true).
But assuming that 0 is a real number, it does follow that positevly indexed arrays would start with it.
I justify it by thinking of it as the position of the element, not the number. Array elements have the same size, which means you could use the number and be just as accurate. But most things that have position do not have identical size, so you have to specify the start position to indicate which one you mean. This fits with the way arrays are stored in C, too, since you can multiple the element size by the index and not waste space.
That doesn’t mean it isn’t confusing though. My string slices are always off by one.
Not to steal your topic Nick, but I thought about a numbering system a few years ago that would recognize two dimensions: space and object. Object would not be considered the only “real” entity. So, to subtract, rather than thinking in terms of “taking away objects” you’d think in terms of the correlation between objects and space.
For example, let’s say you have six total “things” (objects and spaces) laid out like this (where “|” is a space for any object and “X” is an object):
XXX||| (=3[0]/3[s])
There are three objects and three spaces. Now subtraction (on the object plane) can be reduced to the identity of adition (on the space plane): 3[o] - 2[o] (XXX - XX) <=> 3[o] + 2[s] (XXX + ||). Assuming a fixed quantity for the object/space relationship, both equations resolve to the same solution: 1[o]/5[s].
In this construct, zero is unambiguous: 0[o]/0[s] means: absolute nothing, a practiaclly impossible and therefor meaningless entity--do away with it!
Heh. Well that was my attempt as being smart for the decade. I’m starting to smell burning electronics now so I’ll stop. BTW, you have a really nice blog, your AJAX is very responsive!
Wow you guys are really giving this some serious thought. I appreciate the responses, even though the original post was supposed to be linking to some rather humourous discussion.
MonkeeSage: have you read The Nothing That Is?
Think of array indices as distance from the first element. The first element is 0 elements away from itself, the second, 1. This is where the zero-based array comes from.