I am trying to write a model containing digital organisms. Within the framework of the model, I would replace the medium with a fixed 2-dimensional array, but each cell should contain a list of organisms in it. I tried using a jagged array, but as the number of occupied elements varies greatly during program execution, I need to use something more flexible than the array. I tried to make a 2-D array of type list, but im was getting errors with it.
List<Creature>[,] theWorld; public Environment() { List<Creature>[,] theWorld = new List<Creature>[100,100]; } public void addCreature(Creature c) { for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { theWorld[x, y].Add (c); } } }
this is the segment where I try to declare an array at the beginning, as a type that contains lists (from organisms), and then I try to add a creature (c) to each of the lists in each element of the array.
when I run it, I get the following error message -
"In HGT_sim_2.exe
An unhandled exception of type "System.NullReferenceException" occurred.
Additional information: The reference to the object is not installed in the instance of the object. "
and the string "World [x, y] .Add (c);" stands out.
If someone tells me what I'm doing wrong, and even better, a way to solve the problem, it will be awesome. Thank you for the advance!
arrays list c #
Purpleyin
source share