即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

在Ruby中创建二维数组和访问子数组

栈溢出 Andrew Grimm 5℃ 0评论
本文目录
[隐藏]

1.原始问题:Create two-dimensional arrays and access sub-arrays in Ruby

I wonder if there’s a possibility to create a two dimensional array and to quickly access any horizontal or vertical sub array in it?

I believe we can access a horizontal sub array in the following case:

x = Array.new(10) { Array.new(20) }

x[6][3..8] = 'something'

But as far as I understand, we cannot access it like this:

x[3..8][6]

How can I avoid or hack this limit?

2.被采纳答案

There are some problems with 2 dimensional Arrays the way you implement them.

a= [[1,2],[3,4]]
a[0][2]= 5 # works
a[2][0]= 6 # error

2.1.Hash as Array

I prefer to use Hashes for multi dimensional Arrays

a= Hash.new
a[[1,2]]= 23
a[[5,6]]= 42

This has the advantage, that you don’t have to manually create columns or rows. Inserting into hashes is almost O(1), so there is no drawback here, as long as your Hash does not become too big.

You can even set a default value for all not specified elements

a= Hash.new(0)

So now about how to get subarrays

(3..5).to_a.product([2]).collect { |index| a[index] }
[2].product((3..5).to_a).collect { |index| a[index] }

(a..b).to_a runs in O(n). Retrieving an element from an Hash is almost O(1), so the collect runs in almost O(n). There is now way to make it faster than O(n), as copying n elements always is O(n).

Hashes can have problems when they are getting too big. So I would think twice about implementing a multidimensional Array like this, if I knew my amount of data is getting big.

3.1.其他高票答案

3.1.1.第1个答案

rows, cols = x,y  # your values
grid = Array.new(rows) { Array.new(cols) }

As for accessing elements, this article is pretty good for step by step way to encapsulate an array in the way you want:

How to ruby array

3.1.2.第2个答案

You didn’t state your actual goal, but maybe this can help:

require 'matrix'  # bundled with Ruby
m = Matrix[
 [1, 2, 3],
 [4, 5, 6]
]

m.column(0) # ==> Vector[1, 4]

(and Vectors acts like arrays)

or, using a similar notation as you desire:

m.minor(0..1, 2..2) # => Matrix[[3], [6]]

3.1.3.第3个答案

Here’s a 3D array case

class Array3D
   def initialize(d1,d2,d3)
    @data = Array.new(d1) { Array.new(d2) { Array.new(d3) } }
   end

  def [](x, y, z)
    @data[x][y][z]
  end

  def []=(x, y, z, value)
    @data[x][y][z] = value
  end
end

You can access subsections of each array just like any other Ruby array.
@data[0..2][3..5][8..10] = 0
etc

3.1.4.第4个答案

x.transpose[6][3..8] or x[3..8].map {|r| r [6]} would give what you want.

Example:

a = [ [1,  2,  3,  4,  5],
      [6,  7,  8,  9,  10],
      [11, 12, 13, 14, 15],
      [21, 22, 23, 24, 25]
    ]

#a[1..2][2]  -> [8,13]
puts a.transpose[2][1..2].inspect   # [8,13]
puts a[1..2].map {|r| r[2]}.inspect  # [8,13]

转载请注明:CodingBlog » 在Ruby中创建二维数组和访问子数组

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情