Two-Dimensional Arrays

Before reading through this page you will need a clear understanding of what an array is. Please read through the tutorial in the original software development module on arrays here, before reading further.


The arrays you already know are known as single-dimension arrays, thus they only hold single items of data. This can include a list of month names, names on a register or colours in the rainbow. Two-dimensional arrays hold two sets of data, which may sound confusing but it is actually really easy if you think about it. If we were to store the cells in a spreadsheet we have two peices of information that individualise a cell; its row number and its column number. Games often use two-dimensional arrays to represent game boards like Snakes and Ladders, Battleships and Chess.

If your still confused, think of it as an array inside another array. Two-dimensional arrays are no different from the single-dimension ones in the sense that all its data must be of the same type (integers or strings, etc.)


Declaring a 2-dimensional array is similar to the way you declare a one-dimensional array except you indicate that there will be two subscripts. For example, the following are all examples of declarations of 2-dimensional arrays:

int[ , ] intArray;
double[ , ] numbers;
string[ , ] nameArray;

Allocating Memory

Just like one-dimensional arrays, you have to allocate memory for a 2-dimensional array before you can use it. In this case, however, you need to specify how large each dimension is. For example, if you wanted an integer array that represented a table of 3 rows of 6 columns, you might declare it and allocate memory for it as follows:

int [ , ] table = new int[3, 6];

Accessing Elements

When you access elements of a 2-dimensional array, it is similar to how you access onedimensional array elements, except you need to specify both subscripts. For example, to retrieve the value at row 0, column 4, in the table declared in section 1.3, you might have
some code that looks like the following:

int tableEntry;
tableEntry = table[0, 4];
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.