Jagged array
Share:
In this tutorial, we dive into the depths of a programming concept known as the Jagged array in the C# programming language. The concept of arrays is fundamental to any programming language, but as we advance in learning different programming concepts, we stumble upon intricate topics such as multi-dimensional arrays and jagged arrays. To put it simply, a jagged array is an array whose elements are arrays. These elements can be of different dimensions and sizes, unlike multi-dimensional arrays. They are also known as "arrays of arrays".
Let's use a real-world example to demonstrate this. Imagine we have various categories of movies, and each category has a different number of movies. Here, a jagged array can be a superb fit. The various categories can represent the main array, and each movie in a category can be an element array.
Let's start creating it practically in C#.
public class Program
{
public static void Main(string[] args)
{
string[][] movies = new string[3][]; // Create the main array
string[] actionMovies = new string[] { "Die Hard", "John Wick", "Mad Max Fury Road" };
string[] comedyMovies = new string[] { "Anchorman", "Zoolander", "Superbad", "The Hangover" };
string[] romanceMovies = new string[] { "The Notebook", "Before Sunrise", "Titanic" };
movies[0] = actionMovies;
movies[1] = comedyMovies;
movies[2] = romanceMovies;
}
}
We have created an array named 'movies' which will have three elements each of which is an array of strings itself. Each of these arrays can hold separate numbers of elements. The action movies, comedy movies, and romance movies are sub arrays with different numbers of elements. This is something a multi-dimensional array cannot handle, which makes jagged arrays a flexible tool to have in your programming skill-set. The example also shows how you can assign arrays to elements of a jagged array.
After having created it, access of elements inside a jagged array is quite straightforward. If we were to print all the actionMovies from the 'movies' jagged array, here's how it can be done:
for (int i = 0; i < movies[0].Length; i++) // Loop iterating over first element of jagged Array
{
Console.WriteLine(movies[0][i]); // Print each element of the first array
}
Similarly, if we want to print all the movies in all categories, we can employ nested for-each loops as follows:
foreach (string[] movieCategory in movies)
{
foreach (string movie in movieCategory)
{
Console.WriteLine(movie);
}
}
Moreover, using the length property, we can also find out the number of elements in an array as well as in each dimension. For instance, to discover how many categories of movies we have, we can use the 'Length' property:
int totalCategories = movies.Length; // Gives: 3
Console.WriteLine($"Total movie categories: {totalCategories}");
In the case of finding the length of each sub-array, we will have to access each of them separately. Let's try to find the number of comedy movies:
int numOfComedyMovies = movies[1].Length; // Gives: 4
Console.WriteLine($"Number of comedy movies: {numOfComedyMovies}");
Jagged arrays bring in a lot of flexibility with elements of different dimensions and sizes allowed. So far, we have only explored jagged arrays containing one-dimension arrays as elements. But, they are not limited to just that. Jagged arrays can have multi-dimensional arrays as their elements as well. For example:
int[][,] jaggedArray4 = new int[2][,]
{
new int[,] {{1,3}, {5,7}},
new int[,] {{0,2}, {4,6}, {8,10}}
};
In this example, jaggedArray4 is a one-dimensional array containing two elements, which are 2-dimensional arrays of different sizes.
Please remember that while dealing with jagged arrays, you should always check for null values to avoid NullReferenceException. A jagged array element is initialized to null by default if you do not explicitly assign an array to it.
In conclusion, jagged arrays offer much-needed flexibility in terms of sizes and dimensions and prove to be incredibly useful in data management scenarios where this kind of difference in sizes exists. They may seem a bit tricky to understand at first, but once you've got their gist, they are an excellent tool to have in your programming arsenal. The plethora of control offered by them outweighs the complexity which they introduce. Happy Coding!
0 Comment
Sign up or Log in to leave a comment