4

Right now I have a 2d string array holding my data:

//...find number 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?)

9
  • 2
    List<List<string>> would work just fine. Can you think of any operations you'd like to do that wouldn't be straightforward using that? Commented Apr 21, 2011 at 20:14
  • 1
    How do you search (lookup) in this? Commented Apr 21, 2011 at 20:16
  • Can't say that I can, but then I'm not too familiar with them. Just curious what else is out there. Commented Apr 21, 2011 at 20:18
  • @Henk I don't understand your question? Commented Apr 21, 2011 at 20:19
  • emragin, the choice here would be mainly driven by the usage scenarios. You're not showing much of that. Add a few examples. Commented Apr 21, 2011 at 20:21

4 Answers 4

2

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 = ... 
Sign up to request clarification or add additional context in comments.

2 Comments

To answer your question: No. I need the order maintained. Some values may even be empty, but I need them there anyway. The idea of creating a struct to hold my row data would add far greater problems.
That said, I can understand why you'd bring it up--I hadn't made that very clear. The more I think about it, the more an object-oriented approach to the data, which I'd generally be fond of, just isn't right. It's basically to hold a spreadsheet.
1

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"; 

1 Comment

I think that's probably what I'm looking for.
0

Why don't you use Dictionary?

Examples here.

2 Comments

I'd thought about dictionaries in the past. I don't need key-value functionality at all. I need the order maintained, too.
Ok then. If you need more control over ordering than OrderBy, then it's better to use something else :)
0
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>>.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.