-2

What are some good ways to represent a 2D array that extends in arbitrary length in both dimensions?

Working in Ruby.

xpda
  • 15,585
  • 8
  • 51
  • 82
B Seven
  • 44,484
  • 66
  • 240
  • 385

1 Answers1

1

I think a hash is good:

{
  [0, 0] => "A1",
  [1, 0] => "B1",
  ...
  [0, 1] => "A2",
  [1, 1] => "B2",
  ...
}

Or, to make it a less transparent but more efficient, you may think of a way to map a pair of numbers to a single number using a pairing function along the lines suggested here, and use it as a key:

{
  0 => "A1",
  1 => "B1",
  ...
  2 => "A2",
  4 => "B2",
  ...
}
Community
  • 1
  • 1
sawa
  • 165,429
  • 45
  • 277
  • 381