Ruby 多维数组
也许只是我缺乏在这里找到东西的能力,这就是问题所在,但我找不到任何关于如何在 Ruby 中创建多维数组的信息。
有人可以给我一个如何做的例子吗?
Maybe it's just my lack of abilities to find stuff here that is the problem, but I can't find anything about how to create multidimensional arrays in Ruby.
Could someone please give me an example on how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
也许您可以用哈希来模拟多维数组。哈希键可以是任何 Ruby 对象,因此您也可以采用数组。
示例:
基于这个想法,您可以定义一个新类。
只是一个快速场景:
这可以用作:
如果您需要预定义的矩阵 2x2,您可以将其用作:
我可以想象如何处理
<<
或each 在二维数组中,但在多维数组中则不然。
Perhaps you can simulate your multidimensional Array with a Hash. The Hash-key can by any Ruby object, so you could also take an array.
Example:
Based on this idea you could define a new class.
Just a quick scenario:
This can be used as:
If you need a predefined matrix 2x2 you can use it as:
I could imagine how to handle commands like
<<
oreach
in a two-dimensional array, but not in multidimensional ones.记住数组是 ruby 中的一个对象,并且对象不是(默认情况下)简单地通过命名或命名对象引用来创建的,这可能会有所帮助。以下是创建 3 维数组并将其转储到屏幕进行验证的例程:
It might help to remember that the array is an object in ruby, and objects are not (by default) created simply by naming them or naming a the object reference. Here is a routine for creating a 3 dimension array and dumping it to the screen for verification:
这是 ruby 中 3D 数组类的实现,在本例中默认值为 0
Here is an implementation of a 3D array class in ruby, in this case the default value is 0
严格来说,在 Ruby 中创建多维数组是不可能的。但是可以将一个数组放入另一个数组中,这与多维数组几乎相同。
这是在 Ruby 中创建二维数组的方法:
As stated in the comments, you could also use NArray which is a Ruby numerical array library:
使用
a[i][j]
访问数组的元素。基本上,a[i]
返回存储在a
的i
位置的“子数组”,因此a[i][j]
从存储在位置i
的数组中返回元素编号j
。Strictly speaking it is not possible to create multi dimensional arrays in Ruby. But it is possible to put an array in another array, which is almost the same as a multi dimensional array.
This is how you could create a 2D array in Ruby:
As stated in the comments, you could also use NArray which is a Ruby numerical array library:
Use
a[i][j]
to access the elements of the array. Basicallya[i]
returns the 'sub array' stored on positioni
ofa
and thusa[i][j]
returns element numberj
from the array that is stored on positioni
.您可以将一个块传递给 Array.new,
返回该块的值将是第一个数组的每个索引的值,
因此..
您可以使用 array[x 访问该数组][y]
同样对于第二个数组实例化,您也可以传递一个块作为默认值。所以
you can pass a block to
Array.new
the value that returns the block will be the value of each index of the first array,
so..
and you can access this array using
array[x][y]
also for second Array instantiation, you can pass a block as default value too. so
只是澄清一下:
与以下情况完全不同:
在后一种情况下,尝试:
这就是您得到的:
Just a clarification:
is not at all the same as:
in the later case, try:
and this is what you got:
有两种方法可以初始化多数组(大小为 2)。
所有其他答案都显示了带有默认值的示例。
声明每个子数组(您可以在运行时执行):
或在初始化时声明父数组的大小:
使用示例:
因此您可以包装第一种方式并像这样使用它:
There are two ways to initialize multi array (size of 2).
All the another answers show examples with a default value.
Declare each of sub-array (you can do it in a runtime):
or declare size of a parent array when initializing:
Usage example:
So you can wrap the first way and use it like this:
上面给出的方法不起作用。
相当于并且
将打印 1.0,而不是 0.0,因为我们正在修改数组 a 并打印数组 a 的元素。
The method given above don't works.
is equivalent to
and will print 1.0, not 0.0, because we are modifiyng array a and printing the element of array a.
实际上这比上面给出的 block 方法要快得多:
Actually this is much quicker than the block method given above:
最近我不得不在 Ruby 中重现 PHP 风格的多维数组。这是我所做的:
I had to reproduce PHP-style multidimensional array in Ruby recently. Here is what I did: