Click here to Skip to main content
15,908,172 members
Articles / Programming Languages / C#

Jagged Arrays

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
14 Dec 2013CPOL2 min read 9.7K   2  
Jagged arrays

Jagged arrays is one of built-in data structures in .NET Framework where all the elements of the data structure are of the same type. Though it seems simple, but it can easily become puzzling when mixed with other array structures. In this blog, I will try to solve this puzzle.

Let's start with a simple jagged array:

C#
int[][] simple = new int[2][] {new int[] {2, 3, 4}, new int[] {5, 6, 7, 8}};

In the above statement, we declared an array, where each element of the array is itself an array of integers. As part of initialization, we declared the size of the first array as 2. The first element of this array is further initialized with array of size 3 (2,3,4) and the second element is initialized with array of size 4 (5,6,7,8).

Let's make it complex:

C#
int[,][] complex;

What's the outcome of the above statement. Well, there are two possibilities:

  1. We are declaring a single dimension array where each element is itself a matrix of integers.
  2. We are declaring a matrix where each element of the matrix is itself an array of integers.

The correct answer is b. The above statement will create a matrix where each element is an array by itself. The following code snippet will combine the declaration and initialization of the same structure.

C#
int[,][] complex = new int[2, 2][] 
                  { 
                     { new int[] { 1 }, new int[] { 2, 3 } }, 
                     { new int[] { 4, 5, 6 }, new int[] { 7,8,9,10 } } 
                  };

In the above code, we declared a matrix of 2×2. Element at [0,0] is an array of size 1 (1). Element at [0,1] is an array of size 2 (2,3). Element at [1,0] is an array of size 3 with values (4,5,6) and last element [1,1] is an array of size 4 with values (7,8,9,10).

Let's make it more complex.

C#
int[,][,] moreComplex; 

By following the same convention, it is a matrix where each element of the matrix is a matrix of integers. The following code will declare and initialize matrix of matrix:

C#
int[,][,] moreComplex = new int[2,2][,]
                                    {
                                     {
                                      new [,] {{11,12},{13,14}}, 
                                      new [,] {{21,22},{23,24},{25,26},{27,28}
                                     },
                                     {
                                      new [,] {{31,32},{33,34}}, 
                                      new [,] {{41,42}}},
                                    };

Here, we declared a matrix of 2×2. Element at position [0,0] is a matrix of 1×2. Element at position [0,1] is a matrix of 2×2. Element at position [1,0] is a matrix of 1×2 whereas element at position [1,1] is a matrix of 1×1.

Though I will not recommend (as it becomes difficult to maintain and visualize), you can combine jagged arrays with more deeper level to achieve the desired data structure. As an example, the following code is declaring a matrix where each element is a matrix. Each element of sub matrix is a one dimensional array.

C#
int[,][,][] conufusing;
This article was originally posted at http://blogs.malinlab.com/jagged-arrays

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) RBS Bank
United Kingdom United Kingdom
Over 14 years of experience in enterprise / real-time software development using Microsoft Technologies.


More details in LinkedIn Or Blog

Comments and Discussions

 
-- There are no messages in this forum --