Ben Kaufman wrote:
I'm guessing James Gosling, and everyone else who moved Oak into Java.
From the very start - January 23, 1996, wasn't it?
Because it's the right way to do array indexes if you only allow a single
index base.
It allows convenient [start, end) semantics, 'length' represents the next
insertion index, ((rangeEnd - rangeBeginning) == rangeLength), for() loops are
compactly expressible, and it's familiar to anyone trained in Algol-family
languages, just to recap a few of the many answers upthread.
This is an interesting and somewhat religious topic. I'm fairly
agnostic on the choice between the two. Nor am I sure the benefits of
allowing the user to specify the bounds would outweigh the cost of
ambiguity in understanding what an array index means in an arbitrary
context. [
One thing that I believe affects how we see this is our paradigm of
the array index. Is it measuring the distance from the beginning of
the array? Here the zero index makes sense. This is a natural model
for image arrays, or arrays specifying a temporal sequence.
Alternatively an array index can be enumerating a set in which there
is no natural order, e.g., people in an organization or a particle in
a simulation. In this view starting counting at one is natural (in
English at least).
A couple of points in favor of one-indexing that I haven't seen
discussed fully here -- apologies if I missed it.
As the OP's noted ot makes it easier and more natural to indicate the
last element, it's just arr[n] rather than arr[n-1]. Forgetting to
subtract one when looking at the last element is a nit that I
occasionally forget [probably about as often as I would forget the =
in the <= I'd need in the for loop were one-based arrays adopted].
Also, when talking about about arrays either orally or in
documentation, there's a real disconnect when you talk about the first
element and second elements of the array and their indices are 0 and
1. I have heard people talk about the 'zeroth' element -- not
realizing that they are thus suggesting that the 'first' element is
actually the second element. At best this is awkward and at worst it
can lead to real misunderstanding of what's going on. There is no
natural way to match ordinal numbers as used in standard English and 0-
based array indexes.
Tom McGlynn