动态二维数组创建 [英] Dynamic two dimension array creation

查看:116
本文介绍了动态二维数组创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海能any1帮助我如何动态创建二维数组
假设我以以下方式从数据表中检索值

日期值名称
02/02/2011 2 Rajesh
2011/02/02 3拉姆
03/02/20111 8 Rajesh
03/02/2011 6拉克斯克
03/02/2011 3拉姆

现在我的数组应该是这样的

ar [0] [0] = 02/02/2011
ar [0] [1] = 2
ar [0] [2] = 3

ar [1] [0] = 03/02/2011
ar [1] [1] = 8
ar [1] [2] = 6
ar [1] [3] = 3

Hai can any1 help me how to create two dimension array dynamically
Suppose the i retrieve value from datatable in following manner

date Value Name
02/02/2011 2 Rajesh
02/02/2011 3 Ram
03/02/20111 8 Rajesh
03/02/2011 6 Rakesk
03/02/2011 3 Ram

Now my array should be like this

ar[0][0] = 02/02/2011
ar[0][1] = 2
ar[0][2] = 3

ar[1][0] = 03/02/2011
ar[1][1] = 8
ar[1][2] = 6
ar[1][3] = 3

推荐答案

将有两种不同的类型(假设您的元素类型是整数):int[][]int[,].

您几乎不了解主题,所以从2D数组开始(另一种方式允许锯齿状数组):

There will be two different types (let''s assume your element type is integer): int[][] and int[,].

As you hardly understand topic, start with 2D array (the other way allows for jagged array):

int [,] array = new int[4, 12];
array[2, 11] = 212;



锯齿状数组int[][]并不是真正的2D数组:这是整数数组的数组.内部和外部阵列都是一维的.如果您考虑一下这些单词并进行一些试验,您将找到解决它们的方法:初始化外部数组(具有1D秩R的数组的数组),然后执行从0R的循环,并初始化每个内部数组具有任何(不同!)长度的数组.这就是为什么它被称为锯齿状"的原因:内部数组的长度都不同.

—SA



The jagged array int[][] is not really a 2D array: this is an array of integer arrays. Both inner and outer arrays are 1D. If you think about this words and experiment a bit, you will find a way around them: you initialize outer array (array of array with 1D rank R) and then do the cycle 0 to R and initialize every inner array with any (different!) lengths. That''s why it''s called "jagged": inner arrays all have different lengths.

—SA


//Declaration
string[,] arrDateID = new string[5,5];
//Use
arrDateID [0,0] = 02/02/2011;
arrDateID [0,1] = 2;



谢谢,
Imdadhusen



Thanks,
Imdadhusen


请参见此处 [ ^ ].

您已经有了记录数,因此您可以使用
See here[^].

You already have the record count so you can just generate the array by using
int[,] myArray = new int[m,n];


这篇关于动态二维数组创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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