Game Development Reference
In-Depth Information
int [,] grid;
Initializing this array is done as follows:
grid = new int [5,10];
Again, the actual size of the grid is fixed because of the limitation of arrays, but in
general, this is not a problem for grids, which are often used to represent structures
of fixed size such as playing boards. Once we have initialized the array, we can
assign values to it and read from it as follows:
grid[0,1] = 12;
int g = grid[0,1]
grid[0,1];
The Length property is available for multidimensional arrays as well:
int length = grid.Length;
But what does it mean in the case of multidimensional arrays? In this case, Length
will return the size of the first dimension. So in the case of grid , grid.Length will
yield 5. How do we get access to the length of the other dimensions? For this, there
is the GetLength method:
int dim0length = grid.GetLength(0);
int dim1length = grid.GetLength(1);
The array type is not restricted to one- or two-dimensional arrays, for example
we can also declare three- or four-dimensional arrays, if we wish so:
int [,,] grid3d = new int [10,10,10];
int [,,,] grid4d = new int [10,10,10,10];
Although this is all possible to do with arrays, you will probably not use such com-
plicated data structures very often. Also note that such structures start using a lot
of memory. For example, the four-dimensional grid in the previous example does
not look so big (I mean, it is only 10 by 10 by 10 by 10), but it actually contains
10,000 integer variables! With integers this still does not take up a lot of space, but
if you use a more complicated type, such as, say a GameObject , things might look
very different.
We can now extend the syntax diagram representing the different types by in-
cluding the array, as well as classes, structs, and generics such as List :
Search WWH ::




Custom Search