• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why starting index of array is 0

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the reason array index starting from 0 instead of 1
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic