my dog learned polymorphism*
The moose likes General Computing and the fly likes Why array index starts with Zero 0? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "Why array index starts with Zero 0?" Watch "Why array index starts with Zero 0?" New topic
Author

Why array index starts with Zero 0?

Harivenkatesh Polnati
Greenhorn

Joined: Mar 17, 2012
Posts: 25

i have doubt

Why array index starts with Zero 0?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Don’t know. The only people who really know are those who designed Java back in 1994, and we rarely see them here.
Probable reason: because C++ uses 0‑based arrays. And C++ uses them because C uses them. And C uses them because in C an array is a pointer to a memory block containing the data for the array. To get to the beginning you added 0 to the memory location. To get to the 2nd item, you added 1 to the memory location, etc etc. The C compiler can use relative indexing in the CPU to make it very fast to find those memory locations.

This is actually too difficult for “beginning” and is a general question applicable to all languages, so I shall move this discussion. If you search this website or Google, you will doubtless find many other people asking this sort of question, and many of those answers will be helpful to you.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

Harivenkatesh Polnati wrote:Why array index starts with Zero 0?

Because it's a lot easier than having it start with 1; as you'll discover when you get into programming a bit more. Once you get over the learning "hump" you'll probably find that you quite like it.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Winston Gutkowski wrote:
Harivenkatesh Polnati wrote:Why array index starts with Zero 0?

Because it's a lot easier than having it start with 1; as you'll discover when you get into programming a bit more. Once you get over the learning "hump" you'll probably find that you quite like it.

Winston


I still miss the PASCAL approach of being able to define the offset and have real trouble understanding why this feature was not included in Java. I would really love to be able to use a Java array as a Math type vector indexing from [1,N] and not [0,N-1] .
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

To offer a different perspective (), I'm glad that I never have to worry about how the array is declared, because it always starts at zero. And I'm glad that it works like that in every language I'm currently using.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

Richard Tookey wrote:I would really love to be able to use a Java array as a Math type vector indexing from [1,N] and not [0,N-1] .

Why can't you do that now? Just declare it one bigger than you need, and ignore the 0th element.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

Richard Tookey wrote:I still miss the PASCAL approach of being able to define the offset and have real trouble understanding why this feature was not included in Java. I would really love to be able to use a Java array as a Math type vector indexing from [1,N] and not [0,N-1] .

I agree to some extent, especially if the range has some specific meaning (including possibly negative indexes); but there's nothing to stop you writing a class that simulates it. In fact, I've already written a Bounds class for validating indexes between specified bounds.

Winston
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

fred rosenberger wrote:
Richard Tookey wrote:I would really love to be able to use a Java array as a Math type vector indexing from [1,N] and not [0,N-1] .

Why can't you do that now? Just declare it one bigger than you need, and ignore the 0th element.


Yes I could apply that frig but why should I have to and it gets even worse when you want to declare a matrix. The concept of defining the start index for an array has been around a long time; I have used it in APL, ADA and PASCAL . In C and C++ one can usually get round it by pointer shifting and in C++ one can use operator overloading (which for some obscure reason as a concept is very unpopular) .
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Winston Gutkowski wrote:
Richard Tookey wrote:I still miss the PASCAL approach of being able to define the offset and have real trouble understanding why this feature was not included in Java. I would really love to be able to use a Java array as a Math type vector indexing from [1,N] and not [0,N-1] .

I agree to some extent, especially if the range has some specific meaning (including possibly negative indexes); but there's nothing to stop you writing a class that simulates it. In fact, I've already written a Bounds class for validating indexes between specified bounds.

Winston


Wrapping everything in a class is pretty much always the best approach but then one needs operator overloading to allow an intuitive sensible element access syntax.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

I wholeheartedly agree with Matthew. I've spent years programming in Visual Basic, where you can define the bounds of an array whatever you like, and invariably the first thing to do when I plunged into some older code that used arrays was to find out which convention for the lower bound was used.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

Martin Vajsar wrote:I wholeheartedly agree with Matthew. I've spent years programming in Visual Basic, where you can define the bounds of an array whatever you like, and invariably the first thing to do when I plunged into some older code that used arrays was to find out which convention for the lower bound was used.

Yup, I can see that. If you did introduce it, arrays would have to provide a minIndex field as well.

As always, the best solution is a layer of indirection...

I'm still quite happy with my Bounds class though.

Winston
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Winston Gutkowski wrote:As always, the best solution is a layer of indirection...

Yup.

It would be probably quite easy to write a generic List wrapper, which would accept an offset and a list and handle necessary subtraction/addition of the index on the way in and out. I wouldn't be surprised if some of the many open-source collection libraries already contained such a class.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5838
    
    7

Historically, starting indexes with 0 all has to do with addressing. If I have an array, say x[10], then you can think of this as 10 values for x, the first one is at address x (or x + 0 * <size-of-x>), the second at address x + 1 * <size-of-x>, the third at address x + 2 * <size-of-x>, and so on. Thus the "natural" indexes are 0, 1, 2, etc. This makes more sense if you take a machine language or simple assembler class.


JBoss In Action
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Once you are familiar with the C (and hence C++ and hence Java) way of indexing, looping, you'll get accustomed to it.
I must admit to prefer it.



Edsger W. Dijkstra on indexing
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

One of the nice things about constructs like the "for each" loop in various modern languages is that often you no longer have to care if the index starts with 0 or 1.


No more Blub for me, thank you, Vicar.
varshika krish
Greenhorn

Joined: Dec 06, 2012
Posts: 5
it will be taken static value so there will be start in zero
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61450
    
  67

varshika krish wrote:it will be taken static value so there will be start in zero

Static (or not) has nothing at all to do with it. What do you mean by this?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why array index starts with Zero 0?