aspose file tools*
The moose likes Java in General and the fly likes Why starting index of array is 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why starting index of array is 0" Watch "Why starting index of array is 0" New topic
Author

Why starting index of array is 0

Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 88
What is the reason array index starting from 0 instead of 1
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Ganzan Ramakrishnan,
Welcome to JavaRanch.

Well, that's a debateable content .

On serious notes the answer is that it is Java language specification.

Or, you can come up with your logics like "Think of array indices as distance from the first element. The first element is 0 elements away from itself, the second, 1."

Well, maybe zero is the first number while counting
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi,
good reply...
Or, you can come up with your logics like "Think of array indices as distance from the first element. The first element is 0 elements away from itself, the second, 1."

cant understand the explanation of the above content???

thanks
mano
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Well, I wasn't expecting that an explanation for such a weird though will be needed .

Well, I meant that consider that the indexes are the distance of the array elements from the initial element. Thus, the first element is index[0] which is zero positions away from itself. Next is index[1] which is 1 unit away from initial element and so on, next is index[2] which is 2 units away from initial element.

Pretty weird.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
The initial index of arrays and collections varies between computer languages. It's probably fair to say that more languages use zero than use one.

Both approaches have their benefits. Starting at zero maps well onto computer hardware, some algorithms, some types of maths etc. Starting at one maps well onto real-life lists etc.

Java probably went for zero because C and C++ use zero, and Java aims to be easy for C/C++ developers to pick up. C is a language often used for low-level work, so zero was an obvious choice for C.

Someone on JavaRanch has a tag-line that says something like: -
Debate rages on about whether 0 or 1 is the best starting index for arrays. My suggestion of 0.5 was dismissed without, I felt, proper consideration.


I always liked that one.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Originally posted by Peter Chase:
The initial index of arrays and collections varies between computer languages.

Correct. VB(.Net) arrays can start at either 0 or 1, depending on an option set at the start of a file. Pascal and Delphi arrays can have any bounds you want, even negative numbers. These are the only two I know of, however, that do not always start at 0.

But I must agree that using 0 as a base should not be the strict rule. A good example is the Calendar month field. This value is 0 based, with 0 being Calendar.JANUARY, but this has caused many confusion. Especially since weekdays are 1 based, with 1 being Calendar.SUNDAY and 7 being Calendar.SATURDAY. It would make much more sense for months to be 1 based as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

But I must agree that using 0 as a base should not be the strict rule. A good example is the Calendar month field. This value is 0 based, with 0 being Calendar.JANUARY, but this has caused many confusion. Especially since weekdays are 1 based, with 1 being Calendar.SUNDAY and 7 being Calendar.SATURDAY. It would make much more sense for months to be 1 based as well.


Very well said Rob.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Rob Prime:
These are the only two I know of, however, that do not always start at 0.


You must be lucky enough to be young, then. FORTRAN arrays start at one (though I think some later standards, for wussy programmers who like this new-fangled "structured" programming, have the option of zero).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why starting index of array is 0