Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to convert char[][] to char[,] or
List<char[]> to char[,].

please help me for solve this
Posted

You can't necessarily do that.
An array of char[][] or List<char[]>is a jagged array - not all the elements in the collection are defined as being the same length.
For example:
C#
char[][] arr = new char[][] {{'1','2'}, {'1', '2', '3'}, {'1', '2'}};

You can't convert that to a char[,] array because the second dimension size specifier is different for the first and second rows - and char is not a nullable type, so you can't even assign a "default value" to "missing" elements to tidy it up!

It can be done: http://stackoverflow.com/questions/26291609/converting-jagged-array-to-2d-array-c-sharp[^] but there is a good chance you would be better off rethinking what you are doing to create the jagged array in the first place!
 
Share this answer
 
C#
static char[,] To2D(List<char[]> a)
        {
            int Max = 0;
            int aCount=a.Count;
            for (int i = 0; i < aCount; i++) Max=Max<a[i].length?a[i].length:max;>
            char[,] Out = new char[aCount, Max];
            for(int i=0;i<acount;i++)>
                for(int j=0;j<a[i].length;j++)>
                {
                    Out[i, j] = a[i][j];
                }
            return Out;
        }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900