#128 – Accessing Elements in Jagged Arrays

Recall that elements in multi-dimensional rectangular arrays are accessed using an indexer for each dimension, separated by a comma (,):

 // Rectangular array
 int[,] nums1 = new int[2, 3];
 nums1[0, 2] = 20;    // Accessing elements in rectangular array
 nums1[1, 1] = 21;

Elements in jagged arrays are accessed using an indexer for each dimension, but with each indexer enclosed in its own pair of square brackets:

 // Jagged array
 int[][] nums2 = new int[2][];
 nums2[0] = new int[3];
 nums2[1] = new int[2];
 nums2[0][1] = 11;    // Accessing elements in jagged array
 nums2[1][0] = 10;
Advertisement

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: