Hi. I just wanted to get your thoughts on the pros/cons of using a 2d array vs a dictionary of gridposition(keys) paired with gridobjects(balues)
It depends on the use case. For this scenario of a Grid the 2D array makes a lot more logical sense than a dictionary although technically yes that would work.
A grid has a fixed size, which is exactly what we want. Whereas with a Dictionary you can have as many keys as you want, maybe you would forget to set the key gridPosition(2,1) and everything would break without knowing why. Or you would add the gridPosition(-1, -5) and again everything would break without knowing why.
So yes a Dictionary would technically work, but for this use case a regular 2D array is a much better data type for the specific problem we’re trying to solve.
Thanks for taking the time to help. I feel like I understand more now, regarding the grid being fixed, arrays are fixed, just makes sense. About the forgetting to set a key, I don’t see how that differs from an array, although perhaps you mean because the array’s “key” is automatically set by it’s size?
Either way, you don’t need to respond, this helped greatly. Thank you.
Essentially, yes… if you create a GridObject[,] with a given size, you’re guaranteed that any values between 0,0 and (width-1, height-1) are valid locations (but of course, if you don’t put a GridObject within the array at each location, you’ll still get an NRE).
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.