Ruby 多维数组 [英] Ruby multidimensional array

查看:31
本文介绍了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 中创建二维数组的方法:

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天全站免登陆