There is an Array class, but we do not use it explicitly. Instead we use the familiar [] notation. There are a variety of options for arrays.
How to declare an array
We can have a 1-dimensional array:
int [] A; // As yet, A has no size. A = new int[5]; // Now A has a size (which cannot be changed).
or we can combine these:
int [] B = new int[4];
We can initialize the array when it is declared:
int [] C = new int[3] { 1, 2, 3 };
or even shorter:
int [] D = { 4, 5, 6, 7 }; // D has size 4.
We can have a multi-dimensional array:
int [ , ] F = new int[2, 3]; int [ , ,] G = new int [2, 3, 4];
Here one comma indicates 2-dimensional and two commas indicate 3-dimensional.
We can refer to array elements: A[3] or E[1, 2].
Aspects of arrays
The number of dimensions and length of each dimension are established when the array is created and cannot be changed later.
Array subscripts start at 0.
The default value of numeric array elements is 0, and reference elements have the default value null.
We can have an array of any type (including arrays).
Array types are reference types derived from an abstract base class called Array. As the Array class implements the interfaces IEnumerable and IEnumerable<T>, we can use the foreach construct on any array. (With a 2-dimensional array, foreach will proceed through the first row and then the second row, etc.)
Built-in features of arrays
The Array class has various properties including (among others):
Thus, if we have an array such as
int [] H = new int[5] = { 13, 15, 11, 9 };
then H.Length has the value 4 and H.Rank has the value 1.
The Array class also has a variety of methods. Here are a few, all static:
This searches the entire array A for a specific value, It returns -1 for "not found" or the index of the first matching element if there is one.
This sorts the entire array A using the CompareTo method required by the IComparable interface.
This copies elements from Source to Destination.
This searches the array A for the first element for which the predicate Match returns true. It returns that element's value (if found) and the default value of type T (if not found).
This finds all elements of trhe array A for which the predicate Match returns true, and puts them in the returned array. If none are found, the returned array is empty.
For example, we could sort the array H defined above:
Array.Sort(H);
If we wanted to use Array.Sort with an array of some class or struct, we would need the class or struct to implement the IComparable or IComparable<T> interface (which will involve a comparison method).
There are several versions of Sort(); one of them looks like this:
public static void Sort(arrayname, Start, Length)
where Start is an integer = the starting index (usually 0) and Length = the number of elements to be sorted.
There are many more methods, not all static.
Jagged Arrays
A jagged array is an array whose elements are themselves arrays, not all necessarily of the same length. This is declared in the format
type [][] Name;
where we have [][] insted of [,].
Example (adapted from an MSDN page)
int[][] Jag = new int[3][];
Before you can use Jag, its elements must be initialized. You can initialize the elements like this:
Jag[0] = new int[5]; // array of 5 integers Jag[1] = new int[4]; // array of 4 integers Jag[2] = new int[2]; // array of 2 integers
Each of the elements is a single-dimensional array of integers.
We can initialize the array when we create it:
Jag[0] = new int[] { 1, 3, 5, 7, 9 }; Jag[1] = new int[] { 0, 2, 4, 6 }; Jag[2] = new int[] { 11, 22 };
You can also initialize the array upon declaration like this:
int[][] Jag = new int[][] { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22} };
The elements of Jag are arrays and thus are reference types, so they are initialized to null.
You can access individual array elements like this:
Jag[0][1] = 77; // Assign 77 to the second element ([1]) of the first array ([0]):
End of Example
It should be clear that in using a jagged array, we may need to use the Length property.