Sometimes a single dimension array is not enough. Consider a example where you need to store a pattern such as the one below.

You could store this as a single dimension array by saying that the first row starting at (0,0) would be the first value in the array. As there are 3 boxes per row, the box at the start of the second row (1,0) would be at position 3. If the value is black you store a 1 otherwise you store a 0. If you follow these rules then you get the following values.

Pattern(9) = {1,0,1,0,0,0,1,0,1}

Note - The above notation is a quick way of storing values into an array. It is exactly the same as saying pattern(1) = 1, pattern(2) = 0 etc.

However this is a bit confusing. To get the centre block I need to look at position 4 of the array. You get this value by the formula

Position = (row number * 3) + column number.

So to get position (1,1) it would be (1 * 3) + 1 which is 4.

Thankfully there is an easier way of storing a pattern like this. You could use a two dimensional array. This works in the exact same way as a single dimension array. To define a multi-dimensional array you use simply define how many rows and how many columns there are.

Dim X(3,3) As Integer.

The above VB snippit would define a two dimensional array which would easily store the above pattern. Below is the definition of the above pattern.

X(0,0) = 1

X(0,1) = 0

X(0,2) = 1

X(1,0) = 0

X(1,1) = 0

X(1,2) = 0

X(2,0) = 1

X(2,1) = 0

X(2,2) = 1

Now to find out the value of a single square we just need to look up its coordinate. For example to find out (2,1) you can use the code X(2,1) which makes more sense than X(7) which would be equivalent in a single dimension array.

Multidimensional arrays are not restricted to only 2 dimensions. In fact it is possible to have an array of dimension N. The only restriction is the amount of memory available on the computer. One minor thing to point out is that some languages to enforce an artificial limit but this are usually so high that it very rarely becomes a problem. Below are some example definitions of N dimensional arrays.

  • ThreeDPoints(10,10,10)
  • StrangeArray(9,8,7,6)