标签: array-indexing

我如何使用枚举值作为数组的索引

我尝试使用枚举值作为数组的索引,但它给了我一个错误。

export class Color {
    static RED = 0;
    static BLUE = 1;
    static GREEN = 2;
}

let x = ['warning', 'info', 'success'];
let anotherVariable = x[Color.RED]; <---- Error: Type 'Color' cannot be used as an index type.
Run Code Online (Sandbox Code Playgroud)

我尝试了Number()和parseInt转换为数字,但是它不起作用。

有什么方法可以将Enum值用作索引?

javascript arrays enums array-indexing

6
推荐指数
1
解决办法
101
查看次数

指针是否支持“数组样式索引”?

(常见问题解答-此问题不断出现)

我假设读者知道指针算法是如何工作的。

int arr[3] = {1,2,3};
int* ptr = arr;
...
*(ptr + i) = value;
Run Code Online (Sandbox Code Playgroud)

老师/ C书籍不断告诉我,我不应该*(ptr + i)像上面的示例那样使用,因为“指针支持数组样式索引”,而我应该使用它ptr[i] = value;。那里没有论据-更容易阅读。

但是从C标准来看,我什么都没有找到,叫做“数组样式索引”。实际上,运算符[]并不期望任何一个操作数都是数组,而是指针或整数!

6.5.2.1数组下标

约束条件

其中一个表达式的类型应为“完成对象类型的指针”,另一个表达式的类型应为整数,结果的类型应为“ type ”。

为什么数组下标运算符不期望数组?标准错了吗?我的老师/ C书困惑了吗?

c arrays pointer-arithmetic subscript-operator array-indexing

5
推荐指数
1
解决办法
111
查看次数

C# 中 ^1 作为数组索引(例如 arr[^1])是什么意思?

int []arr = new int[4];
arr[^1];   // returns the last element
Run Code Online (Sandbox Code Playgroud)

我正在尝试弄清楚上面的语法。它返回最后一个元素,但为什么呢?

c# syntax array-indexing

5
推荐指数
1
解决办法
1333
查看次数

指定索引处的 OrderedDictionary 键名称

给定一个有序字典,我想获取索引 0 处的键。我当然可以执行一个循环,获取第一次迭代的键并立即跳出循环。但我想知道是否有办法直接做到这一点?我的 Google-Fu 没有发现任何东西,在黑暗中的一些拍摄也失败了。我尝试过类似的事情

$hash[0].Name
Run Code Online (Sandbox Code Playgroud)

$hash.GetEnumerator()[0].Name
Run Code Online (Sandbox Code Playgroud)

我发现了关于在 C# 中执行此操作的讨论,这导致了这个

[System.Collections.DictionaryEntry]$test.Item(0).Key
Run Code Online (Sandbox Code Playgroud)

但这也失败了。这只是无法完成的事情,还是我走错了路?

powershell ordereddictionary array-indexing

4
推荐指数
1
解决办法
793
查看次数

汇编:这个程序中movl data_items(,%edi,4), %eax的作用是什么

该程序(来自 Jonathan Bartlett 的《从头开始编程》)循环访问内存中存储的所有数字,并将.long最大的数字放入 EBX 寄存器中,以便在程序完成时查看。

.section .data
data_items:
    .long 3, 67, 34, 222, 45, 75, 54, 34, 44, 33, 22, 11, 66, 0

.section .text
.globl _start

_start:
    movl $0, %edi
    movl data_items (,%edi,4), %eax
    movl %eax, %ebx
start_loop:
    cmpl $0, %eax
    je loop_exit
    incl %edi
    movl data_items (,%edi,4), %eax
    cmpl %ebx, %eax
    jle start_loop
    movl %eax, %ebx
    jmp start_loop
loop_exit:
    movl $1, %eax
    int $0x80
Run Code Online (Sandbox Code Playgroud)

我不确定(,%edi,4)这个程序的目的。我读到逗号是为了分隔,而 4 是为了提醒我们的计算机数据项中的每个数字都是 4 个字节长。既然我们已经用.long声明了每个数字都是4个字节,为什么我们还需要在这里再声明一次呢?另外,有人可以更详细地解释这两个逗号在这种情况下的用途吗?

x86 assembly att addressing-mode array-indexing

3
推荐指数
1
解决办法
1864
查看次数

“20”[1] 是做什么的?

在一次测试考试中,我们被告知要找出一些表达式的值。

除了 1 之外的所有内容都是明确的,即"20"[1]. 我以为它是数字的第一个索引,所以0,但是用它打印的计算机进行测试48

这个“功能”到底是做什么的?

c arrays array-indexing

2
推荐指数
1
解决办法
96
查看次数

numpy索引:不应该落后省略号是多余的?

在尝试正确理解numpy索引规则时,我偶然发现了以下内容.我曾经认为索引中的尾随省略号没有做任何事情.琐事不是吗?除此之外,事实并非如此:

Python 3.5.2 (default, Nov 11 2016, 04:18:53) 
[GCC 4.8.5] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> 
>>> D2 = np.arange(4).reshape((2, 2))
>>>
>>> D2[[1, 0]].shape; D2[[1, 0], ...].shape
(2, 2)
(2, 2)
>>> D2[:, [1, 0]].shape; D2[:, [1, 0], ...].shape
(2, 2)
(2, 2)
>>> # so far so expected; now
... 
>>> D2[[[1, 0]]].shape; D2[[[1, 0]], ...].shape
(2, 2)
(1, 2, 2)
>>> # ouch!
...
>>> D2[:, …
Run Code Online (Sandbox Code Playgroud)

python numpy array-indexing

1
推荐指数
1
解决办法
139
查看次数