J编程语言中的数组

Dan*_*ebb 5 c++ j

如何使用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中,这些类型的数组访问会是什么样的?

sbl*_*lom 7

在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元素.注意:我们必须将结果分配回来,因为结果只是计算一个新数组,其中包含您使用动词请求的更改.mymy j } m}

y =: j } m
Run Code Online (Sandbox Code Playgroud)

}原因J 的左边只放一个参数来摘录第jth个元素m并将其返回.在这种情况下,我们将y设置为结果.