如何将char [] []转换为char [,] [英] How to convert char[][] to char[,]

查看:132
本文介绍了如何将char [] []转换为char [,]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将char [] []转换为char [,]或

List< char []> to char [,]。



请帮我解决这个问题

解决方案

你不一定这样做。

一个 char [] [] List< char []> 锯齿状数组 - 并非集合中的所有元素都被定义为相同的长度。

例如:

< pre lang =c#> char [] [] arr = new char [] [] {{' 1'' 2'},{' 1'' 2'' 3'},{' 1'' 2'}};



您无法将其转换为 char [,] 数组,因为第一行和第二行的第二个维度大小说明符不同 - 而char不是可空类型,所以你甚至不能为缺失元素分配一个默认值来整理它! />


可以这样做: http://stackoverflow.com/questions/26291609/converting-jagged-array-to-2d-array-c-sharp [ ^ ]但你很有可能会更好地重新思考你正在做的是首先创建锯齿状数组!


  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 < span class =code-keyword> 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;
}


I want to convert char[][] to char[,] or
List<char[]> to char[,].

please help me for solve this

解决方案

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:

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!


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;
        }


这篇关于如何将char [] []转换为char [,]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆