【发布时间】:2016-02-23 22:33:16
【问题描述】:
在 java 或 C# 中,可以使用两个嵌套的 for 循环填充 2d 数组,但在 smalltalk 中,我似乎找不到这样做的方法。 谁能帮帮我?
【问题讨论】:
标签: smalltalk
在 java 或 C# 中,可以使用两个嵌套的 for 循环填充 2d 数组,但在 smalltalk 中,我似乎找不到这样做的方法。 谁能帮帮我?
【问题讨论】:
标签: smalltalk
您可以使用Matrix 并将其创建为:
| random |
random := Random new.
^ Matrix
rows: rowNumber
columns: columnNumber
tabulate: [ :i :j | random next ]
其中 i 和 j 是元素的索引(我在示例中没有使用)
如果你真的想用二维数组做点什么,我建议你做这样的事情:
| random |
random := Random new.
^ (1 to: rowNumber) collect: [ :i |
(1 to: columnNumber) collect: [ :j |
random next ]
创建后也可以遍历矩阵:
| random matrix |
random := Random new.
matrix := Matrix rows: rowNumber columns: columnNumber.
martix indicesCollect: [ :i :j | random next ].
^ matrix
【讨论】: