Michael J Bruesch<br /><i>I code, therefore I am.</i>
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.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"
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
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.Originally posted by G Vanin:
Well, it did not explain why to prohibit any other parallel (and m.b. comfortable to many) alternatives?
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
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"
?
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.
"JavaRanch, where the deer and the Certified play" - David O'Meara
"I'm not back." - Bill Harding, Twister
So if Object obj implements enumeration you can query it using
if (obj instanceof Enumeration){
//do foreach stuff
}
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
}
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.
Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
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.
Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank">here</a> to see an example.
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?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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"?
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |