#132 – Arrays That Are Both Multidimensional and Jagged

Jagged arrays are different than multidimensional arrays.  Multidimensional arrays have a rank greater than one, with a separate indexer and a size for each rank.  Jagged arrays are just an array of arrays.

You can create an array which is both jagged and multidimensional.  Here’s an example (a two-dimensional array of arrays).

 // Multidimensional / jagged array.  E.g. store array
 //   of phone call objects for each day of the week,
 //   each hour of the day.
 PhoneCall[,][] weeklyCallLog = new PhoneCall[7, 24][];

 // Record 2 calls for Monday between 8-9 AM
 weeklyCallLog[1 ,8] = new PhoneCall[2];
 weeklyCallLog[1, 8][0] = thisCall;
 weeklyCallLog[1, 8][1] = thatCall;

Here’s another example (an array of two-dimensional arrays):

 // public enum ChessPiece { Empty, Knight, Rook, Etc };
 ChessPiece[][,] chessGame = new ChessPiece[100][,];
 chessGame[0] = new ChessPiece[8, 8];

#131 – Arrays Derive from System.Array

In C#, all arrays derive from the abstract class System.Array.  You can directly create objects of type System.Array, but must create arrays using the syntax for declaring and instantiating arrays built into the C# language.

Because arrays inherit from System.Array, all arrays inherit certain methods and properties from the Array class.

For example, every array has the properties:

  • Length – Total number of elements, across all dimensions of the array
  • Rank – Number of dimensions in the array
 int[] nums = new int[4];
 int[,] nums2D = new int[2, 3];
 int[][] numsJagged = new int[2][];
 numsJagged[0] = new int[2];
 numsJagged[1] = new int[3];

 int n = nums.Length;    // 4
 n = nums.Rank;          // 1

 n = nums2D.Length;      // 6
 n = nums2D.Rank;        // 2

 n = numsJagged.Length;  // 2
 n = numsJagged.Rank;    // 1

Note that for the jagged array, because the array  is really an array of arrays, Length and Rank operate on the outer array, which is just a 2-element one-dimensional array.

#130 – Default Values of Array Elements

After declaring and instantiating an array, all of its elements take on default values.  The default value for all numeric types is 0 and for all reference types is null.  For enum types, the default value will be the element that has a value of 0–typically the first item listed in the enum.

 float[] nums = new float[4];
 float f1 = nums[0];    // 0.0

 Cat[] pack = new Cat[5];
 Cat c1 = pack[0];      // null

 Days[] someDays = new Days[4];
 Days thatDay = someDays[2];   // Sunday (1st in list)

The same rule applies for default values in a multi-dimensional array.  Each element is 0-valued or null.

 int[,] nums = new int[2, 3];
 int aNum = nums[1, 2];          // 0

With jagged arrays, you typically only instantiate the first dimension.  Elements of that dimension are null.

 int[][] jagged = new int[3][];
 object o = jagged[0];    // null
 int n1 = jagged[0][0];   // Error: NullReferenceException

 jagged[0] = new int[10];
 int n2 = jagged[0][2];    // 0