#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];

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: