开发者

Which array element is the first? [closed]

开发者 https://www.devze.com 2023-01-30 15:01 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 4 years ago.

Improve this question

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-intuiti开发者_运维知识库ve 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?


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".


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...


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.


Anyone saying 'zeroth' must not really believe in zero-based indexing.


The first is the one which is first taken from the stack.

0

精彩评论

暂无评论...
验证码 换一张
取 消