Friday, September 2, 2011

why is that in many languages including java array indices begin with Zero


A  advantage of zero-based array indexes is that this can improve efficiency under certain circumstances. To illustrate, suppose a is the memory address of the first element of an array, and i is the index of the desired element. In this fairly typical scenario, it is quite common to want the address of the desired element. If the index numbers count from 1, the desired address is computed by this expression:
a + s \times (i-1) \,\!
where s is the size of each element. In contrast, if the index numbers count from 0, the expression becomes this:
a + s \times i. \,\!
This simpler expression can be more efficient to compute in certain situations.



No comments:

Post a Comment