Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why array index starts with Zero 0?

 
Harivenkatesh Polnati
Greenhorn
Posts: 25
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have doubt

Why array index starts with Zero 0?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
varshika krish
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it will be taken static value so there will be start in zero
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic