如何使用J编程语言进行数组访问?例如,使用C++作为我的伪代码语言:
int M [100]; // declare an array called M
int j = 5; //index into the array
int y = 10; //value to store or load from the array
M[j] = y; // store y into the array
y = M[j]; // load y from the array
Run Code Online (Sandbox Code Playgroud)
在惯用的J中,这些类型的数组访问会是什么样的?
在J中写这个的文字(但仍然相当惯用)方式如下.
m =: 100 $ 0 NB. This means create a 1d array consisting of 100 zeros.
j =: 5
y =: 10
Run Code Online (Sandbox Code Playgroud)
通过这种初始化,现在我们已经准备好了答案,其中包括两个不同的}
副词用法("Item Amend"和"Amend").
m =: y j } m
Run Code Online (Sandbox Code Playgroud)
在}
原因J 的左侧放置两个参数,用值替换j
右手参数的th元素.注意:我们必须将结果分配回来,因为结果只是计算一个新数组,其中包含您使用动词请求的更改.m
y
m
y j } m
}
y =: j } m
Run Code Online (Sandbox Code Playgroud)
在}
原因J 的左边只放一个参数来摘录第j
th个元素m
并将其返回.在这种情况下,我们将y设置为结果.