3

What is the best way to store table like data like:

         | Column 0 | Column 1 | Column 2 | Column 3 | Column 4
Row 0    | 0.984546 | 0.654564 | 0.467676 | 0.673466 | 50.03333
Row 1    | 0.684546 | 0.457564 | 0.467776 | 0.674566 | 45.73335
Row 2    | 0.884546 | 0.424564 | 0.445676 | 0.664566 | 12.23333
Row 3    | 0.284546 | 0.054564 | 0.237676 | 0.124566 | 45.01333

Both max column and row is dynamic and the coordinates of the value is very important, as I have to do calculation based on those value. It's not a database because the data is different every time and I don't want it to be forever there. Also, I need it to be easily accessible. I try to use LinkedList with Double[] but it's just too difficult for me to locate each of the value. Speed is a factor too.

Any suggestion on the type of collection I should use or ways to handle it?

d219
  • 2,707
  • 5
  • 31
  • 36
  • You could use a `Dictionary>` or a class wrapping it into an indexer. – Florian F. Jun 08 '14 at 00:46
  • 1
    It's not clear what your use cases are of your collection but it's possible this might be one of the few cases where you want to go with a double[,] over a generic collection – Conrad Frix Jun 08 '14 at 01:16

3 Answers3

3

I think you should declare this variable:

List< List<double> > matrix = new List< List<double> >();

Later you can use matrix.Capacity to decide the number of rows and use that same property on each row to set the number of columns. This way you wont have memory wasted.

You can use matrix.TrimExcess() to resize the matrix if your next data will be shorter.

The idea behind using matrix.Capacity and matrix.TrimExcess() is to have control of how much space is reserved in the data structure. This way it wont grow by itself and you will gain performance.

rareyesdev
  • 2,337
  • 1
  • 25
  • 43
2

You can use the .NET DataTable class without using a database at all... You can build one yourself very easily without any datasource.

This way, you still get all of the benefits (you can query it with LINQ, bind it to stuff, still have it be dynamic) but you don't need it to be backed by a database.

Here is a SO answer on how to accomplish that. https://stackoverflow.com/a/1042638/643951

Community
  • 1
  • 1
Ruslan
  • 2,691
  • 1
  • 19
  • 29
0

Another option is to use XML structure

Swagata
  • 622
  • 5
  • 19