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