Which object-collection type to use?
Right now I have a 2d string array holding my data:
//...find n开发者_开发知识库umber of rows needed...
string[,] data = new string[totalRows, 12]; //row / column
It works. BUT now that I want to add more functionality to my program, it's no longer in my benefit since concatenating 2d arrays, while doable, opens up other issues: up until this point I was storing the number of rows in a class variable since I hadn't needed to maintain two at once. Arguably I know that columns is always going to be the same, and I could divide length by that to get rows and write a method to combine them.
I get the feeling that there's a better way to go about this. My knowledge of "newer" stuff is lacking, but I am sure one of them fits the bill better than others. So before I jump in and create List<List<String>> data = new List<List<String>>();
or something else equally weird to look at, I want the opinions of others more experienced.
I do not need any sorting, removing, inserting, etc. functions. I just need it to hold data; and now I need to be able to relatively easily go data += data2
--something to that effect, anyway. data.Length
(giving only the outside length) would also be very useful.
What is the best way to go about this?
Please let me know if you would like any more information. Thanks.
More Info based on answers:
The data is basically of a spreadsheet format. ie.
[['1234-56789-12345', 'screwdriver', '', 'ea', '1', '', '', '', '', '', '', ''],
[['1234-56789-54321', 'wrench', '', 'ea', '1', '2', '3', '', '', '', '', '']]
I do not want to deal with figuring out what the information is describing--I don't care. I need the location relative to everything else.
More info yet:
Usage is as a holding tank between one xml file and another. Just realized that those xlst things might be another solution to my initial problem, but eh. Maybe in another life. (Everything is working.. like I said, adding functionality. If it works, why break it?)
I think a question worth asking is could your columns of data be better represented by an object that explains what each column is holding. For example, instead of
row 1 => { "Jane", "Smith", "1 Rocket Ave", "Houston", "TX" }
You have
new Person { FirstName = "Jane", LastName = "Smith", /* etc. */ }
So then your 2D array becomes a single-dimensional collection of these new Person
objects that are easier to reason about in code.
List<Person> people = ...
// vs.
string[,] people = ...
It depends on how hard or flexible that '12' is.
I would say List<List<string>>
is a flexible approach.
And List<string[]>
would fixate it (could be easier with null columns) while still being flexible about the number of rows. Like a jagged array you need to set it up with a for loop:
List<string[]> data = new List<string[]>();
for (int i = 0; i < desiredRows; i++)
data.Add(new string[12]);
data[1][1] = "Screwdriver";
Why don't you use Dictionary?
Examples here.
Dictionary<string,List<string>>
or
Dictionary<int,List<string>>
or
Dictionary<int,string[]>
etc.
It would depend on what type of key you want to use (strings for names, int for ids, etc.)
If you don't need to lookup by some sort of key, there is nothing wrong with List<List<string>>
.
精彩评论