Sat. Jan 21st, 2023

Single, Two and Multi -dimensional arrays

Arrays are ways to store multiple values using the same variable name. Each value is referred to as an element of the array, and is referenced through use of its index, or in the case of arrays with more than one dimension, indices.

The following conventions apply to all forms of arrays with only very limited exceptions:

  • All items within the array are of the same data type. For example, an array of strings, or integers, or any other class. Regardless of the data type, it will be the same for element 0 as it is for element 1
  • Array indexing begins at zero, not one. That means the first item in the list is item 0, and the second item is item 1 and so on. The only exception to this is Visual Basic which begins at 1.
  • Arrays can not be resized. They are defined with a capacity, and this is fixed. If more elements are required, a new, larger array must be created, and the contents of the old one copied in to it.
  • When getting or setting data within an array, the value representing the element (index) can be a variable. This allows use of a loop to process all items in a list – a massive efficiency win

Arrays use continuous blocks of memory to store their data, hence the requirement to specify the size at the time of creation. For example, if an integer normally requires 32 bits (4 bytes), then creation of an array of 10 integers will reserve a single block of 40 bytes of memory.

As all items within an array are of the same type, and thus a fixed size, they all occupy the same amount of memory (even strings; a string is actually stored as a reference to the memory location containing the string data, and not as the text itself, in case you wondered!). So, as an example, with an integer array called ‘age’ containing 10 entries, what is really happening is:

  • 40 bytes are reserved in memory, and the location of the start of that block is stored in a variable called ‘age’ (note, no brackets)
  • User asks for the first value by using age[0]: the value is found at memory location ‘age’ plus zero * 4 bytes
  • User asks for the fourth item, which is age[3]: the value is found at memory location ‘age’ plus three * 4 bytes

Single-dimension array

The simplest form of an array is one dimensional. These represent simple lists of information – like a track listing on a CD, or a receipt from the supermarket. Each entry (song or purchased item) is an element of the list, and each can be identified by number: for example, what is track number n or the nth item on the shopping list.

In code, this would look like this:

One-dimensional array in C#

string[] names = new string[10];
int[] ages = new int[10];

//Using my own class, Person:
Person[] people = new Person[10];

//Note that the data type of the arrays is defined. 
//Square brackets indicate (in order):
//string array, int array, Person array
//All have been initialised to contain 10 items

names[0] = "Chris";
names[1] = "Claire";
int[0] = 44;
int[1] = 70;

Console.WriteLine("First person is called {0}", names[0]);

As you can see, other than the slight change in notation (the requirement for the square bracket and index, and declaration of capacity when creating), use of arrays is no different to the use of regular variables.

Two-dimensional array

Where a single dimension array can represent a list, a two dimensional array represents a grid structure. Examples could include a chess board, or a bingo grid.

Otherwise the rules are the same as for single dimension arrays.

Please look carefully at this example for additional information:

//Two dimensional array, C#

//Correct way to do it
//If you like, you can think of the indices representing 
//an x and y coordinate

string[,] grid = new string[2,2];
grid[0,0] = "Tom";
grid[0,1] = "Luke";
grid[1,0] = "Sophie";
grid[1,1] = "Yasmine";

//The WRONG way to to it:
string[][] names =...

The “wrong” way is wrong becuase it does not create a two-dimensional array. What would have to follow in the code is:

string[][] names = new string[2];
names[0] = new string[2];
names[1] = new string[2];

See the difference? The ‘wrong’ way creates an array of arrays; while this seems the same, it isn’t as the following would also be valid:

string[][] names = new string[2];
names[0] = new string[5];
names[1] = new string[2];

Each of the arrays is of a different length! This is definitely not a grid.

Other than that, the following is an example of iterating through the contents of a 2D array:

//C#, iterate through 2D array
int[,] grid = new int[6,6];
for (int i=0; i<6; i++)
  for (int j=0; j<6; j++)
    Console.WriteLine("Grid {0}, {1} contains {2}", i, j, grid[i,j]);

//Note we supply 2 indices when referencing elements [i,j] as it's 2D

Multi-dimensional arrays

You can create an array with as many dimensions as you wish by extending the code above. Remember as the number of dimensions increases so too will the memory required to store it!

A three dimensional array represents a cube structure. You could represent a Rubik’s cube using one – if you also created a custom data type called ‘littleCube’ which stored the faces’ colours, you could do something like this:

//3D array, C#

littleCube[,,] rubikCube = new littleCube[3,3,3];
//We have an array containing a 3x3x3 cube of 'littleCube' items

//Access the front, top, left little cube
rubikCube[0,0,0] = new littleCube(...);