为什么Java中的数组索引从0开始?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24841172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is array indexing in Java start with 0?
提问by Shashank Agarwal
Why is array indexingdone with 0 and not with 1 in programming languages like Java ? I am totally new to java any explanation is welcomed.
为什么在 Java 等编程语言中数组索引是用 0 而不是 1 完成的?我对java完全陌生,欢迎任何解释。
采纳答案by Kevin
Java uses zero-based indexing because c uses zero-based indexing. C uses zero-based indexing because an array index is nothing more than a memory offset, so the first element of an array is at the memory it's already pointing to, *(array+0)
.
Java 使用从零开始的索引,因为 c 使用从零开始的索引。C 使用从零开始的索引,因为数组索引只不过是一个内存偏移量,因此数组的第一个元素位于它已经指向的内存中*(array+0)
。
回答by kapex
A quote by Dijkstra, from Why numbering should start at zero (1982):
Dijkstra 的引用,来自Why numbering should startfrom zero (1982):
When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element. Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i< N+1; starting with 0, however, gives the nicer range 0 ≤ i< N. So let us let our ordinals start at zero: an element's ordinal (subscript) equals the number of elements preceding it in the sequence. And the moral of the story is that we had better regard —after all those centuries!— zero as a most natural number.
当处理一个长度为N的序列时,我们希望通过下标区分其中的元素,下一个棘手的问题是给它的起始元素分配什么下标值。遵循惯例 a) 得出,当从下标 1 开始时,下标范围 1 ≤ i< N+1;然而,从 0 开始,给出了更好的范围 0 ≤ i< N。所以让我们让我们的序数从零开始:一个元素的序数(下标)等于序列中它前面的元素数。这个故事的寓意是,我们最好将——在所有这些世纪之后!——零视为最自然的数字。
A discussion about this article can be found in Lambda the Ultimate - Why numbering should start at 0.
关于这篇文章的讨论可以在Lambda the Ultimate 中找到- 为什么编号应该从 0 开始。
回答by Brian
To expand upon @Kevin's answer, I take this quote from an answer on Programmers.SE:
为了扩展@Kevin 的回答,我引用了Programmers.SE 上的一个回答:
The index in an array is not really an index. It is simply an offset that is the distance from the start of the array. The first element is at the start of the array so there is no distance. Therefore the offset is 0.
数组中的索引并不是真正的索引。它只是一个偏移量,即距数组开头的距离。第一个元素位于数组的开头,因此没有距离。因此偏移量为0。
Further, if you want to learn more about how different languages do their array indexing, then look at this exhaustive list on Wikipedia.
此外,如果您想了解有关不同语言如何进行数组索引的更多信息,请查看Wikipedia上的这个详尽列表。
回答by user2041042
It's all legacy from the times when programming languages like C were merely high level assemblers. Programming mavericks spent their wonderful lives doing pointer arithmetic so it became their second nature to count from zero. Now, they are passing this legacy to many modern languages. You can even read statements as "Zero is the most natural number.". Zero is not a natural number. People don't count from zero in real life, mathematicians don't, physicists don't, statisticians don't count from zero... it's only the computer science.
当像 C 这样的编程语言只是高级汇编程序时,这一切都是遗留下来的。编程特立独行者一生都在做指针运算,因此从零开始计数成为他们的第二天性。现在,他们正在将这一遗产传递给许多现代语言。您甚至可以将语句读为“零是最自然的数。”。零不是自然数。人们在现实生活中不会从零开始数,数学家不会,物理学家不会,统计学家不会从零开始……这只是计算机科学。
Further, you don't say "I have zero apples" to express the fact that you don't have any apples, otherwise following same logic, you would say "I don't have minus one apples" to express the fact that you have one apple :P
此外,你不会说“我有零个苹果”来表达你没有任何苹果的事实,否则按照同样的逻辑,你会说“我没有负一个苹果”来表达你没有任何苹果的事实有一个苹果 :P
回答by moeez shem
To summarize his argument:
总结一下他的论点:
When working with sub-sequences of natural numbers, the difference between the upper bound and the lower bound should be the length of the sub-sequence. The indices of an array can be thought of as a special kind of such a sub-sequence. The lower bound should be inclusive, the upper bound should be exclusive. In other words, the lower bound should be the first index of the array. Otherwise, we risk having to have a lower bound in the unnatural numbers for some sub-sequences. If we want to maintain conditions (1) and (2), then we effectively have two choices for upper and lower bounds: 1 <= i < N+1 or 0 <= i < N. Clearly, putting N+1 in the range is ugly, so we should prefer indexing starting from 0.
在处理自然数的子序列时,上界和下界之间的差异应该是子序列的长度。数组的索引可以被认为是这种子序列的一种特殊类型。下限应该是包含的,上限应该是不包括的。换句话说,下限应该是数组的第一个索引。否则,对于某些子序列,我们可能不得不在非自然数中设置下限。如果我们想保持条件 (1) 和 (2),那么我们实际上有两个选择上限和下限:1 <= i < N+1 或 0 <= i < N。显然,将 N+1 放在range 很难看,所以我们应该更喜欢从 0 开始索引。
回答by Devrath
I Have presented answer to this question in the diagram below which i have written in a piece of paper which is self explanatory
我在下面的图表中给出了这个问题的答案,我写在一张纸上,这是不言自明的
Main Steps:
主要步骤:
- Creating Reference
- Instantiation of Array
- Allocation of Data to array
- 创建参考
- 数组的实例化
- 将数据分配到数组
----------
----------
- Also note when array is just instantiated .... Zero is allocated to all the blocks by default until we assign value for it
- Array starts with zero because first address will be pointing to the reference (i:e - X102+0 in image)
- 还要注意什么时候数组刚被实例化......默认情况下,所有块都分配了零,直到我们为它分配值
- 数组从零开始,因为第一个地址将指向引用(即图像中的 X102+0)
Note: Blocks shown in the image is memory representation
注意:图中显示的块是内存表示
I Have also posted this answer here
我也在这里发布了这个答案