This is an old revision of the document!
Arrays
Jagged Arrays
These are arrays of arrays. It only allocated space for each array assigned to it.
// allocate memory int jagged[][] = new int[2][]; jagged[0] = new int[20]; jagged[1] = new int[10]; // use array jagged[0][2] = 15;
Multi-Dimensional Arrays
These are arrays with more than one dimension. Memory gets allocated for the entire reserved space specified by dimensions.
// allocate memory int array[,] = new int[2][10]; // use array array[0,2] = 15;
Getting Dimensions
When traversing an array using loops, for example, there are a couple of ways to do it:
// Single Dimension int SingleDimArr[] = new int[10]; for (int i = 0; i < SingleDimArr.Length; i++) // rows { SingleDimArr[i, 0] = 0; } // Multi-dimensions int DoubleDimArr[,] = new int[2,10]; for (int i = 0; i < DoubleDimArr.GetLength(0); i++) // rows { for (int j = 0; j < DoubleDimArr.GetLength(1); j++) // columns { DoubleDimArr[i, j] = 0; } }
Alternatively, you can traverse without knowing dimensions if you simply need the array values:
int[,] arr = new int[,] {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}}; foreach(int a[] in arr) { foreach(int i in a) { Console.WriteLine(i); } }