# [Interview Q] Why does array index begin with 0 instead of 1 ???

kimsy
Greenhorn
Posts: 5
On one of my recent job interviews, I was asked "In C/C++ and Java, why does the array index start with 0 instead of 1"
For example, if you define the following in C/C++;
int array[10];
It will create an array that can contain maximum 10 integers, and the index goes from 0 to 9 instead of 1 to 10.
I know we are all very used to this convention, but what's the exact reason behind it?
Does anyone know why? Was there any technical reason for this when the C language was created back in 1980? Or is it because of the "B" language that preceded C language back in 1970?

Paul Gupwell
Greenhorn
Posts: 5
The index represents the offset from the beginning of the array.

Michael Bruesch
Ranch Hand
Posts: 158
And as much as that explanation makes some sense, it seems like you're selling the farm just to buy a tractor, kind of pointless. That one particular reason is very valid, but there are many more reasons why it should start with 1, like for a little thing called readability? Now we all know it starts with 0, and we don't think about readability when we see myArray[0] cause we're used to it, but that's just the icing on my proverbial cake. I think this should've been a case of picking the lesser of the two evils, and we chose wrong. But, I'm a programmer, I enjoy a good unreadable challenge.

------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud

Peter den Haan
author
Ranch Hand
Posts: 3252
Originally posted by kimsy:
On one of my recent job interviews, I was asked "In C/C++ and Java, why does the array index start with 0 instead of 1"
Strange question. In C/C++, I would say it's efficiency more than anything else: if p is an pointer of type T, then the memory location of p[n] is p + n*sizeof(T) ('+' here as in straightforward addition, not as in C pointer arithmetic). This is both "cleaner" and (certainly on older CPU architectures) slightly more efficient than the alternative, 1-based convention where the location is p + (n-1)*sizeof(T). Moreover it is closer to the equivalent low-level, assembly-language code. Don't forget that C was conceived as a systems programming language, an alternative for assembler with maximum efficiency in mind.
For Java, these arguments are irrelevant; but Java was designed to be readily accessible to C/C++ programmers, so it makes sense for it to copy the convention.
- Peter

Ranch Hand
Posts: 898
I did not work for ages with C++, but I remember that it was possible to fix ranges and define any starting index (1 or 3).
So, why it is prohibited in Java?

Wilfried LAURENT
Ranch Hand
Posts: 269
We had such a discussion in a thread not so long ago. Here is the link to it. I think we got to the same conclusion as Peter.
http://www.javaranch.com/ubb/Forum33/HTML/003707.html
W.

Ranch Hand
Posts: 898
Well, it did not explain why to prohibit any other parallel (and m.b. comfortable to many) alternatives?

Peter den Haan
author
Ranch Hand
Posts: 3252
Originally posted by G Vanin:
Well, it did not explain why to prohibit any other parallel (and m.b. comfortable to many) alternatives?
Two of the most important design goals of Java are simplicity of the core language, and avoidance of bug-prone language features (eg pointers). Such a feature would go against both. You would need additional array syntax. Developers would need to agree on the array convention to use, and I can tell you now that they won't: you'll inevitably end up with a mix of both conventions, forcing you to look up javadoc when you're not sure, and sneaking in bugs when you think you're sure.
But, nothing prevents you from adding 1 to all your array indices
- Peter

Sean MacLean
author
Ranch Hand
Posts: 621
"kimsy",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
Sean

neal graham
Greenhorn
Posts: 3
Originally posted by kimsy:
On one of my recent job interviews, I was asked "In C/C++ and Java, why does the array index start with 0 instead of 1"
?

I'm curious. What did the person who asked this question say the answer was/is?

Karthik Guru
Ranch Hand
Posts: 1209

probably remotely linked to this. But it just struck me.
It is after seeing these kind of problems that i have managed to appreciate the enumerator pattern.
Just take a look at C#'s foreach. It's simply brilliant.
They have beautifully coupled the enumerator pattern and foreach statement to run through an array or a collection.
Fine VB had foreach and i guess so did perl, but am not so sure if they work the way foreach works in C#.
the for statement in the latest release of python also works the way.
I w'd be interested to know if some other language also implements foreach the same way.
I realised that java badly lacks a Enumerable interface ( to enquire if a collection or an array is enumarable in the first place).

Cindy Glass
"The Hood"
Sheriff
Posts: 8521
huh?
public interface Enumeration
An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series.
For example, to print all elements of a vector v:
for (Enumeration e = v.elements() ; e.hasMoreElements() {
System.out.println(e.nextElement());
}

Methods are provided to enumerate through the elements of a vector, the keys of a hashtable, and the values in a hashtable. Enumerations are also used to specify the input streams to a SequenceInputStream.

So if Object obj implements enumeration you can query it using
if (obj instanceof Enumeration){
//do foreach stuff
}
I realize that hasMoreElements has more letters in it that foreach - but I fail to see the "brilliant" improvement.
Perhaps I am missing something.

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Aside from being more compact and readable, foreach can evidently be applied to arrays, which you can't do directly in Java, since arrays don't implement Collection. The reason they don't is that arrays may be of primitive type - an int[] can't be handled the same way that an Object[] or Integer[] can. It would certainly be possible to add a this functionality to Java and let the compiler take on the trouble of converting the compact foreach notation to something eqivalent which is appropriate for the data dype being accessed. This is not such a big deal for most scripting languages, which eschew strong typing and tend to do most of the work of converting one type to another "under the hood" so you never see it. In some cases "for" is used instead of "foreach" - this might well be necessary in Java to avoid adding a new keyword to the language. Are there existing programs which use "foreach" as an identifier, which would be broken by making foreach a keyword? Unlikely but possible. Then again, overloading a keyword like that seems contrary to existing Java design philosophy (compared to,say, Perl.)
For those who are interested in getting foreach in Java, see JDC bug ID #4280390 and vote accordingly.
Incidentally I feel compelled to point out that the Iterator interfact is a bit slicker-looking than Enumeration, and can be applied to a wider variety of situations (including Vector):
<pre>
for (Iterator it = v.iterator() ; it.hasNext() {
System.out.println(it.next());
}
</pre>
Unless you're stuck with an applet or something that is required to support an archaic JDK version (pre 1.2), there's little reason to keep using Enumeration.

Karthik Guru
Ranch Hand
Posts: 1209

for getting an enumerator for say a ArrayList or a Vector u need to do
Vector v = new Vector()
ArrayList a = new ArrayList()
v.iterator() or
a.iterator() will return the iterator.
By placing the iterator() method in an interface , java c'd've probably established a common way to get an iterator to a collection.

So if Object obj implements enumeration you can query it using
if (obj instanceof Enumeration){
//do foreach stuff
}

I don't understand why a Vector or a ArrayList needs to implement Enumeration interface?. Normally you have an inner class doing it or an anonymous inner class in java.
So i guess the above code w'd evaluate to false in such cases.
i feel the code s'd like this then:
if (obj instanceof Enumerable){
Enumerator e = obj.getEnumerator();
//with the enumerator do for each stuff
}
So is'nt there a need for a standard Enumera"ble" interface (which w'd return an Enumeration object) to check whether the collection is enumerable in the first place?
I guess i s'd'nt be bothered if the Collection implements Enumeration , s'd be fine as long as it implements Enumerable
public interface Enumerable{
Enumeration getEnumerator();
}
As per my understanding,
when using a standard for loop we might mess up with the index
, range and the increment and the enumeration pattern helps us out by defining a set of methods irrespective of the container which holds the objects therby getting rid of possible bugs in the code due to for().
C# goes one step further.
The client does'nt even need to know about the Enumeration interface now. You don't need a while loop too, not knowing about hasMoreElements() is acceptable :-) or the need to call hasMoreElements before a nextElement() need not be known to the client.
just do a
foreach (string s in StringColection).
The foreach w'd run the while for us, it's obvious that it w'd be implemented that way internally.
You can also make your custom collection "foreach"-able by just implementing Enumerable interface. I personally found it very intuitive.
Take a look at the concept of properties in C#, if you have'nt already, is'nt that neat?
someone takes a look at the code and knows that a particular attribute is a property immidiately and that it can be accessed / modified without using a setSomeThing or getSomething.
Fine, indexers are something that they copied from other languages (say python) and called it smart arrays :-)...
I personally found such an improvement pretty cool bcos it's the first time that am observing such a neat design as far as a foreach implementation in a language is concerned. I c'd'nt have appreciated the Enumeration pattern any better.
I found this thing getting implemented in the latest release of python as well. C# already had it when python2.2 came out ! but am not sure about who picked it up from where :-)
So though i termed it as a "brilliant" feature ,you might not find it so :-)
Supporting a foreach in java by writing code is no big deal.
But then OO can be done using C as well i guess.
It feels good to know that the library (by defining a IEnumerable interface ) and the language supports something in an intutive fashion and lends some uniformity.

I think it's generally believed that Java's Collection apis are the worst designed..am not so sure why. Probably someone can elaborate on that. I have read this in lots of discussions. The enumeration thing c'd be a reason.
The other problem which made sense to me was the Stack class inheriting a Vector and not overriding the indexed accessor / mutator methods of Vector in Stack. I believe they s'dve done that and thrown a NotSupportedException.
.NET stack does'nt suffer from such a mess.
More over in C# all arrays (primitive as well as those holding our objects) inherit from System.Array base class which implements Enumerable.
So a "foreach" works for those arrays as well.
So all collections / arrays are treated as one when it comes to enumerating.
As already pointed out, i guess, Enumeration is'nt recommended by Sun because they allow concurrent modification of a collection when someone else is enumerating through it .
An iterator w'd fail immidiately by throwing an exception in a similar situation. Am not aware of other reasons.
But it all depends on our need i guess.
If we are fine with someone doing a dirty read (or whatever is the term for it) then Enumeration interface is fine while in some cases , iterator would probably inform us about a bug(thread safety) in our code by throwing an exception.
BTW, Enumeration interface in .NET behaves the way Iterator does in java.
karthik.
Originally posted by Cindy Glass:
huh?

[B]
So if Object obj implements enumeration you can query it using
if (obj instanceof Enumeration){
//do foreach stuff
}

I realize that hasMoreElements has more letters in it that foreach - but I fail to see the "brilliant" improvement.
Perhaps I am missing something.[/B]

Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Originally posted by karthik Guru:

Just take a look at C#'s foreach. It's simply brilliant.
They have beautifully coupled the enumerator pattern and foreach statement to run through an array or a collection.
Fine VB had foreach and i guess so did perl, but am not so sure if they work the way foreach works in C#.
the for statement in the latest release of python also works the way. I'd be interested to know if some other language also implements foreach the same way.

foreach is a Bill Joy thing; it's been in the Unix C shell since he wrote it back in the early 80's.
For the record, when you want to say "brilliant" or "innovation" and C# is the subject, take a look around. There ain't nuthin' in C# that ain't been taken from somewhere else.

Karthik Guru
Ranch Hand
Posts: 1209
true.
But then C# does improve upon Java as expected ( though not by much but it sure does offer more features/ call it options).
But would'nt you agree that lots of java's features comes from different languages?.
I guess Anders Heljsberg too is a no mean achiever, is he?
apparently was the chief architect of Object Pascal , Delphi @ Borland.

Originally posted by Michael Ernest:
foreach is a Bill Joy thing; it's been in the Unix C shell since he wrote it back in the early 80's.
For the record, when you want to say "brilliant" or "innovation" and C# is the subject, take a look around. There ain't nuthin' in C# that ain't been taken from somewhere else.

Ranch Hand
Posts: 898
Karthik,
due to a lack of time I shall read it later but somehow I would like to mention that "foreach" is not invention of C# and/or VB but of ancestors of OO languages dating back 20-30 yrs already.
[This message has been edited by G Vanin (edited November 08, 2001).]

Francisco A Guimaraes
Ranch Hand
Posts: 182
IMHO, I find the foreach a bit error prone. Please let me finish before I am shot against the wall and executed by C/perl/python fanatics. First, the for statement in Java, accept complex data structures for example:

or if have an array of objects, you can use Arrays.asList() to transform the array.
Francisco

Neil Laurance
Ranch Hand
Posts: 183
Not meaning to start a 'zero-based' vs. 'one-based' war, but 'zero-based' is for hard-core code-with-one-hand-tied-behind-their-back developers, whilst 'one-based' is for wussy-wheres-your-mummy-now scripters.

Layne Lund
Ranch Hand
Posts: 3061
Originally posted by G Vanin:
I did not work for ages with C++, but I remember that it was possible to fix ranges and define any starting index (1 or 3).
So, why it is prohibited in Java?

I started learning C++ almost 10 years ago and have programmed quite a lot with it in the last 5. I am unaware of any such feature in the language. The STL contains classes that can provide something similar, but basic arrays cannot have arbitrary starting indices. They always start at 0.

Ilja Preuss
author
Sheriff
Posts: 14112
foreach will probably come with Tiger (JDK1.5) next year. See JSR 201.

Doanh Nguyen
Ranch Hand
Posts: 45
Originally posted by kimsy:
On one of my recent job interviews, I was asked "In C/C++ and Java, why does the array index start with 0 instead of 1"?

Why not?

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
It pays to be old sometimes...
Back in the late 1700's when Napoleon was converting France to COBOL, the rest of us were still writing BAL (Basic Assembler Language). BAL was written using offsets. Why offsets and not actual position? Because we had one byte to do it in. So the maximum offset that could be expressed was x'FF' or 255. In order to squeeze out the most from this little byte, it was decided to start at 0. This gives you 00-FF as an offset. Other languages simply carried this over since it was always the way that it was done. We did the same with lengths. An MVC instruction (move character) was done with a length of 00-FF. The actual length wasd always one greater than specified. Be glad that wasn't carried over!