java - Why doesn't the index/list of an array begin with 1? -


this question has answer here:

is there special reason? know that's how language has been written, can't change it? , challenges we'd face if index start 1?

for historical reasons, , reasons connected how array "made" in memory.

in c, array piece of memory (with information @ compiler level on size). have pointer (a reference) first element. go second element can do

int array[10]; // array  int *p = array; // reference first element of array int *q = p + 1; // reference second element of array  int *r = p + 2; // reference third element of array  

clearly, symmetry:

array[0] // reference first element of array array[1] // reference second element of array array[2] // reference third element of array 

the [x] operator of c in fact compiled array + x.

you see? array "base 0" in c. , reason in many other languages it's same.

now, c++ has roots in c, java has roots in c++ , other languages, c# has roots in c++, java , other languages... same tree.

basic tree :-)


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -