Ruby多维数组 [英] Ruby multidimensional array

查看:286
本文介绍了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?

推荐答案

严格来说,无法在Ruby中创建多维数组.但是可以将一个数组放置在另一个数组中,这与多维数组几乎相同.

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.

这是您可以在Ruby中创建2D数组的方法:

This is how you could create a 2D array in Ruby:

a = [[1,2,3], [4,5,6], [7,8,9]]


如评论中所述,您还可以使用 NArray ,它是一个Ruby数字数组库:


As stated in the comments, you could also use NArray which is a Ruby numerical array library:

require 'narray'
b = NArray[ [1,2,3], [4,5,6], [7,8,9] ]


使用a[i][j]访问数组的元素.基本上a[i]返回存储在a的位置i上的子数组",因此a[i][j]返回存储在位置i的数组中的元素编号j.


Use a[i][j] to access the elements of the array. Basically a[i] returns the 'sub array' stored on position i of a and thus a[i][j] returns element number j from the array that is stored on position i.

这篇关于Ruby多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆