Jagged Arrays (array-of-arrays)
A jagged array is an array of arrays. Each element in jagged array act as an array. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."
A special type of array is introduced in C#. A Jagged Array is an array of an array in which the length of each array index can different.
Example:
int[][] jaggedArray = new int[5][];
In the above declaration the rows are fixed in size. But columns are not specified as they can vary.
Declaring and initializing jagged array.
int[][] jaggedArray = new int[5][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[6];
jaggedArray[2] = new int[2];
jaggedArray[3] = new int[4];
jaggedArray[4] = new int[10];
jaggedArray[0] = new int[] { 2, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 7, 1, 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 2, 6 };
jaggedArray[3] = new int[] { 2, 4, 6, 45 };
jaggedArray[4] = new int[] { 4, 6, 43, 45, 76, 78, 17, 18, 22, 26, 12 };
You can also initialize the array upon declaration like this:
int[][] jaggedArray = new int[][]
{
new int[] { 2, 3, 5, 7, 9 },
new int[] { 7, 1, 0, 2, 4, 6 },
new int[] { 2, 6 },
new int[] { 2, 4, 6, 45 },
new int[] { 4, 6, 43, 45, 76, 78, 17, 18, 22, 26, 12 }
};
You can use the following shorthand form:
int[][] jaggedArray =
{
new int[] { 2, 3, 5, 7, 9 },
new int[] { 7, 1, 0, 2, 4, 6 },
new int[] { 2, 6 },
new int[] { 2, 4, 6, 45 },
new int[] { 4, 6, 43, 45, 76, 78, 17, 18, 22, 26, 12 }
};
A jagged array is an array of arrays. Each element in jagged array act as an array. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."
A special type of array is introduced in C#. A Jagged Array is an array of an array in which the length of each array index can different.
Example:
int[][] jaggedArray = new int[5][];
In the above declaration the rows are fixed in size. But columns are not specified as they can vary.
Declaring and initializing jagged array.
int[][] jaggedArray = new int[5][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[6];
jaggedArray[2] = new int[2];
jaggedArray[3] = new int[4];
jaggedArray[4] = new int[10];
jaggedArray[0] = new int[] { 2, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 7, 1, 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 2, 6 };
jaggedArray[3] = new int[] { 2, 4, 6, 45 };
jaggedArray[4] = new int[] { 4, 6, 43, 45, 76, 78, 17, 18, 22, 26, 12 };
You can also initialize the array upon declaration like this:
int[][] jaggedArray = new int[][]
{
new int[] { 2, 3, 5, 7, 9 },
new int[] { 7, 1, 0, 2, 4, 6 },
new int[] { 2, 6 },
new int[] { 2, 4, 6, 45 },
new int[] { 4, 6, 43, 45, 76, 78, 17, 18, 22, 26, 12 }
};
You can use the following shorthand form:
int[][] jaggedArray =
{
new int[] { 2, 3, 5, 7, 9 },
new int[] { 7, 1, 0, 2, 4, 6 },
new int[] { 2, 6 },
new int[] { 2, 4, 6, 45 },
new int[] { 4, 6, 43, 45, 76, 78, 17, 18, 22, 26, 12 }
};
No comments:
Post a Comment