C# – Multidimensional Arrays & Jagged Arrays
Multidimensional Arrays
A multidimensional array is an array that has two or more dimensions, and an individual element is accessed through the combination of two or more indices.
The simplest form of the multidimensional array is the two-dimensional array. In a twodimensional array, the location of any specific element is specified by two indices. If you think of a two-dimensional array as a table of information, one index indicates the row, the other indicates the column. To declare a two-dimensional integer array table of size 10, 20, you would write
int[,] table = new int[10, 20];
To access an element in a two-dimensional array, you must specify both indices, separating the two with a comma. For example, to assign the value 10 to location 3, 5 of array table, you would use
table[3, 5] = 10;
If a two-dimensional array has a size of m by n, there are exactly m*n elements.
int[,] matrix = { {1, 2, 3, 4}, {5, 6, 7, 8}, };
The array matrix has 8 elements, stored in 2 rows and 4 columns. Each element can be accessed in the following way:
matrix[0, 0] matrix[0, 1] matrix[0, 2] matrix[0, 3] matrix[1, 0] matrix[1, 1] matrix[1, 2] matrix[1, 3]
using System;
class TwoD {
static void Main() {
int t, i;
int[,] table = new int[3, 4];
for(t=0; t < 3; ++t) {
for(i=0; i < 4; ++i) {
table[t,i] = (t*4)+i+1;
Console.Write(table[t,i] + " ");
}
Console.WriteLine();
}
}
}
Jagged Arrays
Jagged arrays are arrays that instead of primitive types, contain arrays (or other collections). It’s like an array of arrays – each array element contains another array.
They are similar to multidimensional arrays, but have a slight difference – as multidimensional arrays are limited to a fixed number of rows and columns, with jagged arrays, every row can have a different number of columns.
Jagged arrays are declared by using sets of square brackets to indicate each dimension. For example, to declare a two-dimensional jagged array, you will use this general form:
type[ ] [ ] array-name = new type[size][ ];
Here, size indicates the number of rows in the array. The rows, themselves, have not been allocated. Instead, the rows are allocated individually. This allows for the length of each row to vary. For example, the following code allocates memory for the first dimension of jagged when it is declared. It then allocates the second dimensions manually.
int[][] jagged = new int[3][]; jagged[0] = new int[4]; jagged[1] = new int[3]; jagged[2] = new int[5];
After this sequence executes, jagged looks like this:
using System;
class Jagged
{
static void Main() {
int[][] jagged = new int[3][];
jagged[0] = new int[4];
jagged[1] = new int[3];
jagged[2] = new int[5];
int i; // Store values in first array.
for(i=0; i < 4; i++)
jagged[0][i] = i; // Store values in second array.
for(i=0; i < 3; i++)
jagged[1][i] = i;
// Store values in third array.
for(i=0; i < 5; i++)
jagged[2][i] = i; // Display values in first array.
for(i=0; i < 4; i++)
Console.Write(jagged[0][i] + " ");
Console.WriteLine(); // Display values in second array.
for(i=0; i < 3; i++)
Console.Write(jagged[1][i] + " ");
Console.WriteLine(); // Display values in third array.
for(i=0; i < 5; i++)
Console.Write(jagged[2][i] + " ");
Console.WriteLine();
}
}
Jagged arrays are not used by all applications, but they can be effective in some situations. For example, if you need a very large two-dimensional array that is sparsely populated (that is, one in which not all of the elements will be used), then a jagged array might be a perfect solution.
One last point: Because jagged arrays are arrays of arrays, there is no restriction that requires that the arrays be one-dimensional.