如何在Ruby中声明2维数组。我知道,V=[]
是一维的。但{2}的v=[][]
?在一个块中,我想在数组中添加值作为子数组。即V=[["ab","ba"]
,["12","21"]]
。这就是我在做的事情。让x=[]
。我拿每个元素,存储原始元素和反向元素。
x.each{|k| l=k_reverse v=(k,l)}
答案 0 :(得分:4)
# Given
list = ["ab","12"]
# This should give you an array of arrays
v = list.map{|x| [x,x.reverse] }
# v = [["ab","ba"],["12","21"]]
二维数组可能被初始化为
v = [[]] # not [][], [][] would be the reader for a 2 dim array