array.length – 1 在数组末尾是如何可视化理解的?

huangapple go评论149阅读模式
英文:

Can someone help me visually understand how array.length - 1 is at the end of the array?

问题

I am trying to solve a binary sort problem in Java. I can't wrap my head around why the end of the array would be array.length - 1.

if

int[] array = [0,1,2,3,4]

int start = 0;
int end = array.length - 1;

Wouldn't end return 3?

英文:

I am trying to solve a binary sort problem in Java. I can't wrap my head around why the end of the array would be array.length - 1.

if

int[] array = [0,1,2,3,4]

int start = 0;
int end = array.length - 1; 

Wouldn't end return 3?

答案1

得分: 5

Explanation

术语 索引(类似于位置)从 0 开始计数。

所以你的数组,有 5 个元素,这些元素在索引 0, 1, 2, 3, 4 处。因此,最后一个元素确实在索引 4 处,即 length - 1

所以可以可视化地看,对于像 { 3, 6, 1, 8, 4 } 这样的数组,我们有

值:  3 6 1 8 4
索引: 0 1 2 3 4

背景

我想,至少在 Java 和许多其他编程语言中,索引从 0 开始计数的最强大的原因之一与其在底层是如何实现的有关,就像这样:

array[i]

基本上被计算为

sizeof(type) * i

以获取实际的内存地址偏移量。并且在这里使用 0 会导致偏移量为 0,从而指向该数组的内存开头。

实际上,也有一些不采用这种方式的编程语言,比如 Matlab,那里的 索引1 开始计数。

英文:

Explanation

The term index (something like position) starts counting at 0.

So your array, which has 5 elements, has elements at indices 0, 1, 2, 3, 4. So the last element is indeed at index 4, which is length - 1.

So visualized, for an array like { 3, 6, 1, 8, 4 } we have

values:  3 6 1 8 4
indices: 0 1 2 3 4

Background

I suppose one of the strongest reasons why the indices (at least in Java and many other languages) start counting at 0 has to do with how it is implemented under the hood, as in:

array[i]

basically being computed as

sizeof(type) * i

to get hands on the actual memory address offset. And having 0 there would result in an offset of 0, so the beginning of the memory for this array.

There are actually languages that did not go this route, like Matlab where indices start counting at 1.

答案2

得分: 1

Array length would be 5, since there are 5 elements in the array.

Element 0 is the 1st element (index 0), element 1 is the second (index 1), ..., element 4 is the fifth (index 4).

So the index of the last element is 5-1 = 4.

英文:

Array length would be 5, since there are 5 elements in the array.

Element 0 is the 1st element (index 0), element 1 is the second (index 1), ..., element 4 is the fifth (index 4).

So the index of the last element is 5-1 = 4.

答案3

得分: 1

这个列表的长度为5。但最后一个索引是4,因为第一个索引是0(不是1)。

英文:

This list is of length 5. The last index is 4, though, because the first index is 0 (not 1).

答案4

得分: 1

array.length 是 5。

在Java中,数组的计数从0开始,所以 array[0] 是 0。

逐个递增,你会发现该数组的最后一个元素(如你所定义)的索引位置是4,即 array.length - 1

英文:

array.length is 5.

In Java, you start counting arrays at 0, so array[0] would be 0.

Count upwards, and you'll find that the last element of that array, (as you have defined it) has index position 4, ie array.length - 1

答案5

得分: 0

Indexes start at 0.

Sizes/Lengths start at 1.

Array length = 5;

Last element is in Index the length - 1 (the difference between sizes/lengths indexes).

英文:

Indexes start at 0.

Sizes/Lengths start at 1.

Array length = 5;

Last elemnt is in Index the length - 1 (the difference between sizes/lengths indexes.

huangapple
  • 本文由 发表于 2020年7月22日 21:44:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63035655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定