Ruby multidimensional array

前端 未结 11 1442
春和景丽
春和景丽 2020-11-28 09:30

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 s

11条回答
  •  暖寄归人
    2020-11-28 09:40

    The method given above don't works.

    n = 10
    arr = Array.new(n, Array.new(n, Array.new(n,0.0))) 
    arr[0][1][2] += 1
    puts arr[0][2][2]
    

    is equivalent to

    n = 10
    a = Array.new(n,0.0)
    b = Array.new(n,a)
    arr = Array.new(n, b) 
    arr[0][1][2] += 1
    puts arr[0][2][2]
    

    and will print 1.0, not 0.0, because we are modifiyng array a and printing the element of array a.

提交回复
热议问题