为什么array.slice表现不同的(长度,N) [英] Why does array.slice behave differently for (length, n)
本文介绍了为什么array.slice表现不同的(长度,N)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个数组 A
:
-
A [则为a.length]
收益无
。不错。 -
A [则为a.length,X]
收益[]
。不错。 -
A [则为a.length + X,Y]
收益无
。不符合2。
a[a.length]
returnsnil
. Good.a[a.length, x]
returns[]
. Good.a[a.length+x, y]
returnsnil
. Inconsistent with 2.
虽然这种行为是记录,这似乎很奇怪。
While this behavior is documented, it seems odd.
谁能解释一下这个设计背后的原因是什么?
Can anybody explain the reasons behind this design?
推荐答案
考虑这个
a = [0, 1, 2, 3] #=> [0, 1, 2, 3]
a[0, 10] #=> [0, 1, 2, 3]
a[1, 10] #=> [1, 2, 3]
a[2, 10] #=> [2, 3]
a[3, 10] #=> [3]
a[4, 10] #=> []
a[5, 10] #=> nil
所以 A [4,10]
是和数组的一端之间的切片 3
[]
So a[4, 10]
is the slice between the 3
and the end of the array which is []
在哪里为 A [4]
和 A [5,10]
正在访问不在元素数组
Where as a[4]
and a[5, 10]
are accessing elements that aren't in the array
有可能有助于认为限幅点的作为在元件之间存在,而非元件本身。
It may help to think of the slice points as being between the elements, rather than the elements themselves.
这篇关于为什么array.slice表现不同的(长度,N)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文