5
Reply

How to best implement a 2D sparse array of data?

steve

steve

Jul 9 2008 3:04 AM
10.4k

To the more experienced C# programmers, how do I do this best?

I have a 2 dimensional mapping of values in a database, which represent a random sampling of a somewhat round (but not exactly round) shape when drawn out on paper. That is, the data is in the form of a "row" coordinate and a "column" coordinate, and a data value for that set of coordinates. If this was a 100% sampling, the data would look like a filled circle when plotted on paper. Since it is only a random sampling, not all of the coordinates belonging to the filled circle will be present.

Before I retrieve the data from the database, I do not know the maximum value of either the row or column coordinate. I can know that first when I retrieve all the data from the database and find the maximum values for both coordinates.

My question is what is the best way to store this data in my C# program? In a 2D rectangular array? This may be wasteful because the data is sparse. In an array of hashtables where the array key is the row coordinate and the hashtable key is the column coordinate? In an array list of sorted lists?

Important is that I can access the value quickly for any combination of row and column coodinates, as this will happen very often in the program.

Thanks for any tips!


Answers (5)