[.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.

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 on other sites
Quote:
 Original post by davidcolemanMaybe 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 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 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");}