Arrays

C# does something interesting with arrays, as compared to how they're built and used in both C++ and Java. In those languages, you define each dimension of the array with a corresponding pair of square brackets. In C++, though, you can also define the size of the array inside the brackets. Here (as with Java), you need to use the new keyword. Like so:

Slacker[] array       = new Slacker[10]; // A 1-D array of 10 Slacker objects
Slacker[ , ] array_2d = new Slacker[10, 10]; // A 2-D array of 100 slacker objects


With as many dimensions as we are bold enough to attempt to keep straight, defined by subsequent pairs of square brackets. With C#, declaration of multidimensional arrays and what are called "jagged" arrays bring an extra level of granularity to the table, which can often be quite useful. Let's first look at how multidimensional arrays are addressed:

int[] first = new int[100];
int[] second = new int[5] { 0, 1, 2, 3, 4 }; // initial array values
int[] third = { 5, 6, 7, 8, 9 }; // array size equal to 5, with these initial values

int[ , ] first_2d = new int [3, 3]; // 3x3 array of integers
int[ , , ] first_3d = new int [2, 2, 2]; // 2x2x2 array of integers


Thankfully, we still refer to values from an array using the same square bracket notation we should be familiar with: first[0] (the first element of the first array) and first_3d[0, 0, 0] (the first element, of the first row, of the first column, of the first... uh, height? No, that's not right...). There are also properties of Arrays that we can utilize, such as Length (total number of elements within an Array) and rank (number of dimensions of an Array). So for example:

System.Console.WriteLine(first.Length); // Would print 100
System.Console.WriteLine(first_2d.Rank); // Would print 2


The Array class also contains some useful methods, such as:

int Array.BinarySearch(Array yourArray, Object target); // Bin-Search an array for a target value
void Array.Sort(Array yourArray); // Sort the entire array using the CompareTo method
// required by the IComparable Interface. More on that later.




Jagged arrays function more like an array of Vectors or ArrayLists, wherein each Vector/ArrayList doesn't necessarily have to contain the same size as all the others. Each dimension of the array beyond the first, can contain a programmer-defined number of elements, and you do have to define that upfront (as opposed to the dynamically-sized Vectors and ArrayLists). The notation for defining jagged arrays is exactly the same as multidimensional arrays in C++/Java, though, so you have to be careful going forward.

int[][] jaggedArray = new int[3][]; // Three integer arrays of yet undefined sizes

jaggedArray[0] = new int[100]; // The first array will contain 100 integers
jaggedArray[1] = new int[5]; // The second will contain 5 integers
jaggedArray[2] = new int[42]; // The last array will be empty. Just kidding: it'll have 42 integers


You can also initialize a jagged array like so:

Slacker[][] alpha = new Slacker[][]
{
new Slacker[] { EINS, ZWEI, DREI },
new Slacker[] { VIER, FUNF, SECHS, SIEBEN },
new Slacker[] { ACHT, NEUN }
};


The initial values for those Slacker objects shouldn't make sense to you. It doesn't even make sense to me!

Similar to defining/accessing multidimensional arrays, we will access the values of jagged arrays using the [][]...[][] notation.

System.Console.WriteLine(Slacker[1][2]); // Should print SECHS