如何用`#数组[] =`负折射率的工作? [英] How does negative index work with `Array#[]=`?

查看:114
本文介绍了如何用`#数组[] =`负折射率的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看阵列#[] = 工作,并发挥各地:

 枚举[INT] = OBJ→OBJ
枚举[开始,长度] = OBJ→OBJ
枚举[系列] = OBJ→OBJ

问题1

我有一个数组 B 控股在其 0 指数。

  B = []
B [0]#= GT;零

我试着用替换整数 10 在下面的code。

  B [-1] = 10#=> IndexError:指数-1太小阵列;最低:0

为什么没有code以上的工作,但下面的那些呢?在与大小的数组的情况下, 1 ,为什么指数 0 1 区别对待?

  B [0] = 5#=>五
B〔-1〕= 10#=> 10

问题2

我创建大小 2 的数组,并做了以下内容:

  A = [1,2]一个[-3] = 3#=> IndexError:指数-3太小阵列;最低:-2
一个[-3] = [3]#= GT; IndexError:指数-3太小阵列;最低:-2
一个[-3 ..- 4] = [3]#= GT;引发RangeError:-3 ..- 4超出范围

我相信,负指数不增加数组的大小,但我不知道为什么。为什么code以下成功吗?

  A [-2 ..- 3] = [3,4]#=> [3,4]


解决方案

我建议你看一看在的阵列文档。它令人惊奇地说:一个负索引被假设为相对于所述阵列的就是说的结束,的索引-1表示阵列的最后一个元素,-2是阵列中的下一个到最后一个元素,因此在

这意味着,您可以设置 A [-N] 个元素,当且仅 | N | < = a.size 。这就是为什么 A = [1,2];一个[-3] = 3 失败(3> 2)。

在另一方面,对于红宝石阵列[可能]没有记录功能: A [INBOUNDS_IDX..NONSENSE_IDX] = SMTH 插入 SMTH 之前 INBOUNDS_IDX 指数:

  A = [1,2]
一个[2..0] ='A'
一个[2..1] ='B'
A [2 ..- 100] ='c'的
#⇒[1,2,C,B,一个]
一个[2 ..- 1] ='Q'
#⇒[1,2,Q]

在这里胡说八道的意思是小于INBOUNDS_IDX,不作为治疗的在负符号指数(这就是为什么 A [2 ..- 1] 在上面的例子中被视为 A [2 ..(a.size - 1)]。

I tried to see how Array#[]= works, and played around:

enum[int] = obj → obj
enum[start, length] = obj → obj
enum[range] = obj → obj

Question 1

I have one array b holding nil at its 0 index.

b = []
b[0]   # => nil

I tried to replace nil with integer 10 in the code below.

b[-1] = 10 # => IndexError: index -1 too small for array; minimum: 0

Why doesn't the code above work, but the ones below do? In case of an array with size 1, why are the indices 0 and -1 treated differently?

b[0] = 5   # => 5
b[-1] = 10 # => 10

Question 2

I created an array of size 2, and did the following:

a = [1,2]

a[-3] = 3       # => IndexError: index -3 too small for array; minimum: -2
a[-3] = [3]     # => IndexError: index -3 too small for array; minimum: -2
a[-3..-4] = [3] # => RangeError: -3..-4 out of range

I believe that negative index never increases the size of an array, but I don't know why. Why did the code below succeed?

a[-2..-3] = [3,4] #=> [3, 4]

解决方案

I would suggest you to take a look at the first para in Array documentation. It surprisingly says: "A negative index is assumed to be relative to the end of the array—that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on."

That means, that you may set a[-N]th element if and only |N| <= a.size. That’s why a = [1,2] ; a[-3] = 3 fails (3 > 2).

On the other hand, there is [likely] not documented feature for ruby arrays: a[INBOUNDS_IDX..NONSENSE_IDX]=SMTH will insert SMTH before INBOUNDS_IDX index:

a=[1,2]
a[2..0]='a'
a[2..1]='b'
a[2..-100]='c'
# ⇒ [1, 2, "c", "b", "a"]
a[2..-1]='q'
# ⇒ [1, 2, "q"]

Nonsense here means "less than INBOUNDS_IDX, and not treatable as index in an negative notation" (that’s why a[2..-1] in the example above is treated as a[2..(a.size - 1)].)

这篇关于如何用`#数组[] =`负折射率的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆