Gilbert said:
I was asked this weekend by one of our trainee programmers, why for a list with a single entry does list.size() report '1' but to access it you need to use list.get(0). Now it's a convention that I have accepted for years without really thinking about it - and he has a point, it is slightly counter-intuitive. It's not always been like this, I remember when I started programming in the mid 19<mumbles> on IBM gear using RPG & RPGII, that array indexing started at 1.
Now I suspect that when Java was designed, an earlier convention was followed. I'm not sure that I would believe that it was a capricious design decision - so does anyone know who decided that indexing from zero was the way to go, when, and most interestingly why?
Regards
1. Next time could you be so kind as to use an normal
newsgroup-textformat an not to make longlines which needs hor-scrolling?
That makes it nearly impossible to read and to answer to it.
Thanks.
2. If an array / collection starts with Index 0 or 1 has nothing to do
with the size of the array and even if there is only 1 element or
thousands in it.
The decision to define the first Index by 0 or 1 is just a matter of
taste of those guys who invented it. Both sides have advantages:
- starting with 0: you will make the experience that working with
indices of an array you'll need less use of using offset (adding /
subtracting 1)
- starting with 1: seems to be a little more intuitive. But that's only
the force of habit. If you work with the "0" for a long time and switch
to 1 than you will just think the other way round
3. When Java was designed they had several design goals. Starting with 0
or 1 has - in my opinion - not a lot to do with design: simple think
about it a short while an than decide. And in this case I suppose they
just took over the 0 from C.
Design is -in this case- something like "don't let us make the same
mistakes again" that leads to things like GC (no nees to clean up the
memory yourself) and avoiding multiple inheritance.
By the way: some programming languages allow it to specify the
start-index of an array as you like: e.g. array starts at index -42.
That's just a gimmick and capricious, not really necessary.
Regards, Norbert