Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which array element is the first? [closed]

A few times during discussion about programming, I reached a misunderstanding, caused by different views on how consecutive zero-based array elements are referred to using ordinal numerals. There seem to be two views on that:

a[0] = "first";
a[1] = "second";
a[2] = "third;

vs:

a[0] = "zeroth";
a[1] = "first";
a[2] = "second";

I always preferred the first, knowing that "n-th" element is "element of index n-1". But I was surprised how many people found that counter-intuitive and used the latter version.

Is one of those conventions more correct than the other? Which should I use during discussion or documentation to avoid misunderstanding?

like image 278
Kos Avatar asked Dec 13 '10 21:12

Kos


3 Answers

I think the English meaning of the word "first" is unambiguous, and refers to the initial element of a sequence. Having "first" refer to the successor of the initial element is just wrong.

In cases where there might be confusion, I would say "the third element, at index 2".

like image 139
Greg Hewgill Avatar answered Nov 12 '22 06:11

Greg Hewgill


The element index is pretty much language-dependent (e.g. C: 0, Lua: 1), whereas the fifth element is the fifth element, it's just the index that may be different ;)

I guess that's way too diffuse an answer...

like image 42
Marcus Borkenhagen Avatar answered Nov 12 '22 08:11

Marcus Borkenhagen


In some languages, such as Pascal, you can specify the range of indexes explicitly. i.e.

var stuff : array[-3..3] of integer;

stuff[-3] is still the first element in the array, not the negative third.

like image 3
Ferruccio Avatar answered Nov 12 '22 07:11

Ferruccio