Sign in to follow this  

[.net] C# multidimensional array of List?

This topic is 3306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Maybe something else would be simpler but I need something like this: So on a each coordinate on a map 1000 x 1000 to be able to have 0 or 1000 elements? I want to be able to do something like myMap[120,15].Add("tree")

Share this post


Link to post
Share on other sites
Quote:
Original post by davidcoleman
Maybe something else would be simpler but I need something like this:

So on a each coordinate on a map 1000 x 1000 to be able to have 0 or 1000 elements?

I want to be able to do something like

myMap[120,15].Add("tree")


Something like this?

System.Collections.Generic.List<string>[,] myMap = new System.Collections.Generic.List<string>[1000, 1000];

myMap[120,15].Add("tree");


Share this post


Link to post
Share on other sites
I get this message

            
System.Collections.Generic.List<string>[] myMap = new System.Collections.Generic.List<string>[1000];
for (int i = 0; i < 1000; i++)
myMap[i].Add("tree");


NullReferenceException was unhandled

Share this post


Link to post
Share on other sites
For reference types, you still need to new individual elements of an array before using them. It should look something like:

System.Collections.Generic.List<string>[] myMap = new System.Collections.Generic.List<string>[1000];
for (int i = 0; i < 1000; i++) {
myMap[i] = new System.Collections.Generic.List<string>();
myMap[i].Add("tree");
}

Share this post


Link to post
Share on other sites
Sign in to follow this