C#将ArrayList转换为2D int数组 [英] C# convert ArrayList to 2D int array

查看:110
本文介绍了C#将ArrayList转换为2D int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#


是否可以将ArrayList转换为2D int [1,13]数组?



ArrayList是所有数字,如1111100000000,0111110000000。

我希望它们看起来像这样:



int [, ] a = {{1,1,1,1,1,0,0,0,0,0,0,0,0},{0,1,1,1,1,1,0,0,0 ,0,0,0,0},{0,0,1,1,1,1,1,0,0,0,0,0,0},{0,0,0,1,1,1} ,1,1,0,0,0,0,0}};





我想要转换它们的原因是如果带有ArrayList的条件,我不知道如何创建多数组:



if(a [i,j]> = b [i,j] ])

{

做点什么...

}

c#
Is it possible to convert a ArrayList to a 2D int[1,13] array ?

The ArrayList is all numbers like "1111100000000", "0111110000000".
And I want them to look like this:

int[,] a = { { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 } };


The reason I want to convert them is that I don't know how to create multi-array if conditions with an ArrayList like:

if(a[i,j] >= b[i,j])
{
do something...
}

推荐答案

您可以使用Linq创建一个int [],[]数组:

You could use Linq to create an int[],[] Array:
// required
using System.Linq;

int[],[] jaggedArray = aList.ToArray().Select(elem => elem.ToString().Select(ch => (int)Char.GetNumericValue(ch)).ToArray()).ToArray()

// or

int[],[] jaggedArray = aList.ToArray().Select(elem => elem.ToString().Select(ch => ch - '0').ToArray()).ToArray()

// test
int val12 = jaggedArray[1][2];;

注意:CharGetNumericValue(Char)返回Type'Double,表示如果使用它使用Unicode Ch ar,你可能会得到一小部分回来!



我同意Sergey的观点,'ArrayList不再是明智的选择......如果有可能......你应该使用列表< List< int>>

Note: CharGetNumericValue(Char) returns a Type 'Double which means if you use it with a Unicode Char, you might get a fraction back !

I agree with Sergey that 'ArrayList is no longer a wise choice ... if it's possible ... you should use a List<List<int>>


这篇关于C#将ArrayList转换为2D int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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