Answers for "3 dimensional jagged array c#"

C#
2

jagged array c#

// C# program to illustrate the declaration 
// and Initialization of Jagged Arrays
using System;
  
class GFG {
      
    // Main Method
    public static void Main()
    {
          
        // Declare the Jagged Array of four elements:
        int[][] jagged_arr = new int[4][];
  
        // Initialize the elements
        jagged_arr[0] = new int[] {1, 2, 3, 4};
        jagged_arr[1] = new int[] {11, 34, 67};
        jagged_arr[2] = new int[] {89, 23};
        jagged_arr[3] = new int[] {0, 45, 78, 53, 99};
  
        // Display the array elements:
        for (int n = 0; n < jagged_arr.Length; n++) {
  
            // Print the row number
            System.Console.Write("Row({0}): ", n);
  
            for (int k = 0; k < jagged_arr[n].Length; k++) {
  
                // Print the elements in the row
                System.Console.Write("{0} ", jagged_arr[n][k]);
            }
            System.Console.WriteLine();
        }
    }
}
Posted by: Guest on February-18-2022
0

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.");
    } 
}
Posted by: Guest on October-17-2020
0

jagged array c#

jagged array is also known as varriable size array. Can store data of any size.

int[][] jagged_arr = new int[3][];
jagged_arr[0] = new int[] {1, 7, 9, 22};
jagged_arr[1] = new int[] {11, 34, 67};
jagged_arr[2] = new int[] {110, 24};
Posted by: Guest on April-07-2022

C# Answers by Framework

Browse Popular Code Answers by Language