Why does C#.NET have so multiple ways of defining and using multi-dimensional arrays?
Jagged Arrays (C# Programming Guide)
Jagged arrays have the same declaration style as Java’s multidimensional arrays, and the same idea, but they don’t seem to have the same ease of use! In C Sharp, you must initialise each array in turn…
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
jaggedArray[0][0] = 77; // etc
// or
// Declare the array of two elements:
int[][] arr = new int[2][];
// Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };
And then you can go ahead and use them!
class ArrayTest
{
static void Main()
{
// Declare the array of two elements:
int[][] arr = new int[2][];
// Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
/* Output:
Element(0): 1 3 5 7 9
Element(1): 2 4 6 8
*/
Multidimensional Arrays (C# Programming Guide)
Multidimensional arrays behave more like Java multidimensional arrays, except you don’t seem able to have a variety of different length arrays - that would be a jagged array!
int[,] array = new int[4, 2];
// or
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
class ArrayTest
{
static void Main()
{
// Declare the 2 dimensional array:
int[,] arr = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i,j], j == (arr[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
Update: If you came here looking for a way to write a multi-dimensional array in C. Well it is obviously very different.
Single and multi-dimensional arrays in C:
Single dimensional array (remember to initialise it!):
int listofnumbers[50];listofnumbers[0]=100;
listofnumbers[1]=100;
// and so on
Multidimensional array (also requires initialisation):
int tableofnumbers[50][50]; tableofnumbers[0][0]=100; // etc
In C you could also construct the space required using pointers and malloc, but that would be slightly different. A good reference manual online for C is Programming in C; UNIX System Calls and Subroutines using C. I found it very helpful when I was at University learning about this stuff. Fortunately (or unfortunately) I haven’t had reason to need it for a while.










4 Comments
Ya, tell me about it
And what about 3d arrays ??
Multi-dimensional arrays are similar to 2d arrays. Just add another dimension.
For 3d arrays
int[,,] array = new int[4, 2, 1];
// or
int[,,] array3D = new int[,,] { { {1},{2}}, { {3}, {4} }, { {5}, {6} }, { {7}, {8} } };
Set individual elements:
array3D[0,0,0] = 1;
how can i initialize 0 to 255 characters in two dimentional arrays??