The Artima Developer Community
Sponsored Link

Java Answers Forum
Why arrays start with index 0 everywhere?

4 replies on 1 page. Most recent reply: Mar 13, 2002 8:45 PM by anil

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
jinaraj

Posts: 8
Nickname: bullet
Registered: Feb, 2002

Why arrays start with index 0 everywhere? Posted: Mar 8, 2002 4:42 AM
Reply to this message Reply
Advertisement
Hi
This is a very basic question. In all the languages I am familiar with, arrays start with index 0. Even Java is following this. Is there any reason behind it?


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Why arrays start with index 0 everywhere? Posted: Mar 8, 2002 10:24 AM
Reply to this message Reply
Well, I think when they originally suggested the idea of starting them at 37, there was public outcry, so they decided not to do it. ;-)

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

PowerBuilder Starts array index at 1 Posted: Mar 8, 2002 11:59 AM
Reply to this message Reply
In PB you can start your array index at any integer you want to. Even negative integer. By default, it starts at 1. I think the first language which introduced array started index at 1 and others continued it.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: PowerBuilder Starts array index at 1 Posted: Mar 8, 2002 2:07 PM
Reply to this message Reply
That sounds like the kind of "feature" that makes me glad I have never done any code maintenance on PowerBuilder projects. I would hate to work on code where some goofball thought it would so clever to have negative indices for some arrays, while others start at random numbers. It is much better to not have ambiguity about such things.

On the other hand, Python allows negative indices, but they have a specific and very useful meaning: they allow you to access elements backward, from the end of the list. Python also has slices, which in conjunction with negative indices is amazingly powerful and and allows you to solve some problems very simply and cleanly compared to other languages (except those that also have these features!).

To answer the original question seriously, the reason arrays in many modern languages start with 0 (instead of 1) if probably because the seminal influence of the C programming language, where an array index is an offset from the beginning address of the array. Therefore, the first element would have a no (zero) offset. Because many programmers are accustomed to this, languages like Java, which could have chosen either 0 or 1 as the first element of an array, choose 0.

I know from experience working with Delphi and Visual Basic that it is annoying to use to 1-based arrays after using 0-based (in C/C++) for many years, but if the whole language works that way, you can quickly get used to it. Since Borland's excellent C++ Builder product is based on Delphi, some of the 1-based collections show through; this makes for a even more confusion because you are working with some 1-based collections in a 0-based language. However, that is a minor inconvenience and I still like C++ Builder better than any other tool for C++ development.

anil

Posts: 1
Nickname: friend
Registered: Mar, 2002

Re: Why arrays start with index 0 everywhere? Posted: Mar 13, 2002 8:45 PM
Reply to this message Reply
index to be choosen as 0 in c language necessarily as index.as in memory we take the base address of array in index 0 & further access it by moving the pointer as in c language.as there is no such things exits i.e array[1],array[2] etc. in memory.it do so by the help of pointers.c came very before to java.

but in java we adopted the same convention from these languages.and computer takes two basics things 0 & 1.therefore it starts from 0

Flat View: This topic has 4 replies on 1 page
Topic: Problem with jar archive Previous Topic   Next Topic Topic: Connection Access Denied (HELP!!)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use