Kalamata
Kalamata

Reputation: 1

How am I able to instantiate multiple Objects in a 2D array of 1D arrays in C#

So I'm trying to have an array of 2D arrays (so [,][])

They all return Null in the end though for some reason (Most likely due to no assigning to the array)

Edit1: Mistake fixed in code. Thanks. Edit2: Fixed title, as it is in fact a 2D array of 1D arrays.

Example:

public Environment()
{
    _grid2D = new Object[20, 20][];
}

I also try to assign objects to them later in my code:

public GenerateGrid() 
{
    Random rand = new Random();
    for (int i = 0; i < 10; i++ ) 
    {
        obj = new InsertObject(rand.Next(0,19), rand.next(0,19));
        _grid2D[InsertObject.XPos, InsertObject.YPos][0] = obj;
    }
}

I am attempting to use this kind of array because I require seemingly multiple planes of 2D, that are kind of stacked on top of eachother. This way multiple game objects can technically exist in the same space, as the 2D array is an array that will contain positions (X and Y properties already defined elsewhere)

This may be a little convoluted, as there is maybe a better approach.

I need a 20x20 grid, with multiple planes of this grid.

Randomly deciding the location is a design choice, and when the time comes that there are multiple objects in the same location, I will check for this and prevent / reassign a location (rand again).

Upvotes: 0

Views: 103

Answers (2)

ES2018
ES2018

Reputation: 438

Two different implementations one with a 3D array and another is a list of 2D arrays

    static void Main(string[] args)
    {
        int numberOfPanes = 50;
        var myGrid1 = GenerateGrid1(20, 20, numberOfPanes);
        var myGrid2 = GenerateGrid2(20, 20, numberOfPanes);


    }

    public static Object[,,] GenerateGrid1(int x, int y, int numberOfPanes)
    {
        var grid = new Object[x, y, numberOfPanes];            

        Random rand = new Random(Guid.NewGuid().GetHashCode());

        for (int k = 0; k < numberOfPanes; k++)
        {
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    grid[i, j, k] = rand.Next(1, 20);
                }
            }
        }

        return grid;
    }


    public static List<int[,]> GenerateGrid2(int x, int y, int numberOfPanes)
    {
        var grid = new int[x, y];
        var multiPanes = new List<int[,]>();

        Random rand = new Random(Guid.NewGuid().GetHashCode());

        for (int k = 0; k < numberOfPanes; k++)
        {
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    grid[i, j] = rand.Next(1, 20);
                }
            }

            multiPanes.Add(grid);
        }

        return multiPanes;
    }

Upvotes: 0

Wyck
Wyck

Reputation: 11730

I'm guessing, but I think what you are trying to do is to allow any number of "InsertObject" objects in each cell of the 2D array. I'm guessing, based on your access pattern of randomly selecting x,y coordinates for each object as you add them to the grid.

If that is the case, then use a List in each cell of the grid. If you want, you can allocate each List when you add the first item, and leave it sparse (so you don't create lists for cells with 0 objects in them.) Or you can do a first pass where you populate all the cells of the 2D array with empty lists. The strategy you go with depends on how much you care about efficiency, and whether you expect a sparse or dense population.

List<Object>[,] _grid2D = new List<Object>[20, 20];
Random rand = new Random();
for (int i = 0; i < 10; i++)
{
    int x = rand.Next(1, 20);
    int y = rand.Next(1, 19);
    Object obj = new object(); // Replace with your InsertObject here.
    if (_grid2D[x, y] == null) // If this cell's list doesn't exist yet...
    {
        _grid2D[x, y] = new List<Object>(); /// ... then make one.
    }
    _grid2D[x, y].Add(obj); // Add the object to the list.
}

Just be careful when accessing the grid if you go with this sparse technique, as some grid cells may have no List created if they have 0 objects (_grid2D[x,y] may be null).


And if you don't want to allow multiple objects per grid cell, then you just need a 2D array of InsertObject objects. InsertObject[,] _grid2D = new InsertObject[20,20];

Upvotes: 1

Related Questions