jagged array in c# code example

Example 1: jagged array c#

// A "multidimensional array" is essentially a matrix occupying one block of memory
// The "jagged array" is an array of arrays 
//  - with no restriction that the latter arrays need to be of the same dimension/length
//  - each of these latter arrays also occupy their own block in memory

int[][] jArray = new int[3][]{
  					new int[2]{1, 2},
					new int[3]{3, 4, 5},
                	new int[4]{6, 7, 8, 9}
            	};

jArray[1][2]; //returns 4

// We can change a whole element of array
jArray[1] = new int[3] { 10, 11, 12 }; 

jArray[1][2]; //returns 11

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

intJaggedArray[0][1][2]; //returns 6

// https://stackoverflow.com/a/4648953/9034699
// https://www.tutorialsteacher.com/csharp/csharp-jagged-array



// The following are multidimensional arrays
// Two-dimensional array. (4 rows x 2 cols)
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// Three-dimensional array. (4 rows x 2 cols)
int[,,] array3D = new int[,,] { 
  									 { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } },
                                     { { 13, 14, 15, 16 }, { 17, 18, 19, 20 }, { 21, 22, 23, 24 } } 
                                   };
int[,,] array3D = new int[2, 3, 4] { 
  									 { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } },
                                     { { 13, 14, 15, 16 }, { 17, 18, 19, 20 }, { 21, 22, 23, 24 } } 
                                   };

// C# also has jagged arrays

Example 2: how t declare a jagged array c#

data_type[][] name_of_array = new data_type[rows][]

Example 3: jagged array java

int[][] numbers = new int[7][];

// row 0 gets 5 columns
numbers[0] = new int[5];
// row 1 gets 11 columns
numbers[1] = new int[11];
// etc...

Example 4: how to declare 2d array in c#

// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

Example 5: jagged array to 2d array c#

static T[,] To2D<T>(T[][] source)
{
    try
    {
        int FirstDim = source.Length;
        int SecondDim = source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular

        var result = new T[FirstDim, SecondDim];
        for (int i = 0; i < FirstDim; ++i)
            for (int j = 0; j < SecondDim; ++j)
                result[i, j] = source[i][j];

        return result;
    }
    catch (InvalidOperationException)
    {
        throw new InvalidOperationException("The given jagged array is not rectangular.");
    } 
}