-2

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

Working in Ruby.

xpda
  • 15,322
  • 8
  • 50
  • 80
B Seven
  • 42,103
  • 65
  • 226
  • 370

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
  • 160,959
  • 41
  • 265
  • 366