This Tutorial Explains All About Multidimensional Arrays & Jagged Arrays in C# With Examples. Multidimensional arrays are also known as Rectangular Arrays:
We explored all about Arrays and Single Dimensional Arrays in our previous tutorial.
In this tutorial, we will learn about Multi-Dimensional Arrays and Jagged Arrays in C# in detail along with examples.
=> Explore Our In-Depth C# Training Tutorials Here
Table of Contents:
C# Multi-Dimensional Arrays
Multi-dimensional arrays are also known as rectangular arrays. Multi-dimensional arrays can be further classified into two or three-dimensional arrays.
Unlike single-dimensional arrays where data is stored in a liner sequential manner, a multi-dimensional array stores data in tabular format i.e. in the form of rows and columns. This tabular arrangement of data is also known as a matrix.
2-Dimensional Arrays
The simplest form of multidimensional array is a two-dimensional array. A two-dimensional array can be formed by stacking several one-dimensional arrays together. The following figure will help in understanding the concept better.
The above image is a graphical representation of how the 2-dimensional array looks like. It is denoted by having a row and column. Hence, each building block of the two-dimensional array will be made up of the index representing row number and column number.
Multidimensional arrays are declared like the single-dimensional array with the only difference being the inclusion of comma inside the square bracket to represent rows, columns, etc.
string[ , ] strArray = new string[2,2];
Now, let’s have a look at an example to initialize a two-dimensional array.
A 2-D array is declared by
string [ , ] fruitArray = new string [2,2] { {“apple” , “mango”} , /* values for row indexed by 0 */ {“orange”, “banana”} , /* values for row indexed by 1 */ };
For Example, let’s say if my array element has “i” row and “j” column then we can access it by using the following index array[i, j].
string [ , ] fruitArray = new string [2,2] { {“apple” , “mango”} , /* values for row indexed by 0 */ {“orange”, “banana”} , /* values for row indexed by 1 */ }; /* output for the elements present in array*/ for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { Console.WriteLine("fruitArray[{0},{1}] = {2}", i, j, fruitArray[i,j]); } } Console.ReadKey();
The output of the following program will be:
fruitArray[0,0] = apple
fruitArray[0,1] = mango
fruitArray[1,0] = orange
fruitArray[1,1] = banana
Explanation:
The first part of the program is the Array declaration. We declared a string type array of row size 2 and column size 2. In the next part, we tried to access the array using for loop.
We have used a nested for loop for accessing the values. The outer for loop provides the row number i.e. it will start with the “zeroth” row and then move ahead. The inner for loop defines the column number. With each row number passed by the first for loop, the second for loop will assign a column number and access the data from the cell.
Jagged Arrays In C#
Another type of array that is available with C# is a Jagged Array. A jagged array can be defined as an array consisting of arrays. The jagged arrays are used to store arrays instead of other data types.
A jagged array can be initialized by using two square brackets, where the first square bracket denotes the size of the array that is being defined and the second bracket denotes the array dimension that is going to be stored inside the jagged array.
Jagged Array Declaration
As discussed a jagged array can be initialized by the following syntax:
string[ ][ ] stringArr = new string[2][ ];
A jagged array can store multiple arrays with different lengths. We can declare an array of length 2 and another array of length 5 and both of these can be stored in the same jagged array.
Filling Element Inside Jagged Array
Lets first initialize a Jagged Array.
arrayJag[0] = new string [2] ; arrayJag[1] = new string [3] ;
In the above example, we have initialized a string type jagged array with index “0” and “1” containing an array of size defined inside the square bracket. The 0th index contains a string type array of length 2 and the index “1” contains a string type array of length 3.
This was how we initialize an array. Let’s now initialize and put values inside a jagged array.
arrayJag[0] = new string [2] {“apple”, “mango”}; arrayJag[1] = new string [3] {“orange”, “banana”, “guava”};
Hence, as shown in the above example, the jagged array can also be declared with values. To add values, we place a curly bracket after the declared jagged array with the list of values.
It is also possible to initialize the jagged array while declaring it.
This can be done by using the following approach.
string[][] jaggedArray = new string [2][] { new string[] {“apple”, “mango”}, new string[] {“orange”, “banana”, “guava”} };
In the above example, we defined a Jagged array with name “jaggedArray” with size 2 and then inside the curly bracket we defined and declared its constituent arrays.
Retrieve Data From A Jagged Array
Until now we learned about putting data inside a Jagged array. Now, we will discuss the method to retrieve data from a Jagged array. We will use the same example that we discussed earlier and will try to retrieve all the data from that array.
string[][] jaggedArray = new string [2][] { new string[] {“apple”, “mango”}, new string[] {“orange”, “banana”, “guava”} }; /* retrieve value from each array element */ for (int i = 0; i < jaggedArray.Length; i++) { for (int j = 0; j < jaggedArray[i].Length; j++) { Console.Write(jaggedArray[i][j]+ “ ”); } Console.WriteLine(); } Console.ReadKey();
The output of the following program will be:
apple mango
orange banana guava
Explanation:
We used two for loops to transverse through the elements. The first for loop defined the index for the Jagged array. Another nested for loop was used to transverse through the array present in the given jagged array index, then we printed the result to console.
Points to Remember:
- A jagged array is an array of arrays. i.e. it stores arrays as its values.
- The jagged array will throw out of range exception if the specified index doesn’t exist.
Conclusion
In this tutorial, we learned about Jagged and Multidimensional arrays in C#. We learned how to declare and initialize a two-dimensional array. We also created a simple program to retrieve data from a two-dimensional array.
Then we discussed in detail about Jagged array, which is an array of arrays.
A Jagged array is unique in itself as it stores arrays as values. Jagged arrays are quite similar to other arrays with the only difference being the type of value it stores.
=> FREE C# Training Tutorials For All