Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert multidimensional array to jagged array in C#

I have a C# WCF webservice which is called by two VB 6 project. The target VB project is sending to the client VB project a multidimensional array.

I want to convert the multidimensional array to a jagged array but i have no luck.

How can i find the number of olements in my object[,] to be able to initialize the jagged array ?

I want to follow the answer from this question but i don't have a GetLength method on my object.

I tried :

int firstElement = astrManTfrLetters.GetLength(0);
int secondElement = astrManTfrLetters.GetLength(1);

And i stuck here.

like image 489
sebastian.roibu Avatar asked Feb 24 '14 11:02

sebastian.roibu


3 Answers

Usually the solutions presented assume 0-based indices but that's not always the case, mainly if on the client you are dealing with object[,]'s for Microsoft Excel.

Here is a solution for any indices:

internal static class ExtensionMethods
{
    internal static T[][] ToJaggedArray<T>(this T[,] twoDimensionalArray)
    {
        int rowsFirstIndex = twoDimensionalArray.GetLowerBound(0);
        int rowsLastIndex = twoDimensionalArray.GetUpperBound(0);
        int numberOfRows = rowsLastIndex + 1;

        int columnsFirstIndex = twoDimensionalArray.GetLowerBound(1);
        int columnsLastIndex = twoDimensionalArray.GetUpperBound(1);
        int numberOfColumns = columnsLastIndex + 1;

        T[][] jaggedArray = new T[numberOfRows][];
        for (int i = rowsFirstIndex; i <= rowsLastIndex; i++)
        {
            jaggedArray[i] = new T[numberOfColumns];

            for (int j = columnsFirstIndex; j <= columnsLastIndex; j++)
            {
                jaggedArray[i][j] = twoDimensionalArray[i, j];
            }
        }
        return jaggedArray;
    }
}
like image 184
Pedro Avatar answered Oct 17 '22 05:10

Pedro


By default, the C# produces the 0-based array. I have fine-tuned Pedro's solution as below:

internal static class ExtensionMethods
{
    internal static T[][] ToJaggedArray<T>(this T[,] twoDimensionalArray)
    {
        int rowsFirstIndex = twoDimensionalArray.GetLowerBound(0);
        int rowsLastIndex = twoDimensionalArray.GetUpperBound(0);
        int numberOfRows = rowsLastIndex - rowsFirstIndex + 1;

        int columnsFirstIndex = twoDimensionalArray.GetLowerBound(1);
        int columnsLastIndex = twoDimensionalArray.GetUpperBound(1);
        int numberOfColumns = columnsLastIndex - columnsFirstIndex + 1;

        T[][] jaggedArray = new T[numberOfRows][];
        for (int i = 0; i < numberOfRows; i++)
        {
            jaggedArray[i] = new T[numberOfColumns];

            for (int j = 0; j < numberOfColumns; j++)
            {
                jaggedArray[i][j] = twoDimensionalArray[i + rowsFirstIndex, j + columnsFirstIndex];
            }
        }
        return jaggedArray;
    }
}
like image 29
Ray Chen Avatar answered Oct 17 '22 05:10

Ray Chen


This worked for me and did not require any looping. It took a object[85000,26] and converted it to object[85000][26] in a little over a second.

object[,] obj2D = ...

// Take my 2D array and cast it as a 1D array
object[] obj1D = ((object[,]) obj2D).Cast<object>().ToArray();

// using linq, chunk the 1D array back into a jagged array
Int32 j = 0;
object[][] jagged = obj1D.GroupBy(x => j++ / obj2D.GetLength(1)).Select(y => y.ToArray()).ToArray();
like image 2
Kevin M Avatar answered Oct 17 '22 04:10

Kevin M