Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why starting index of array is 0

 
Ganesan Ramakrishnan
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reason array index starting from 0 instead of 1
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic