您的位置:首页 > 运维架构

Hashtable.Keys.CopyTo(Array,int)方法只能拷贝一维数组吗?能不能拷贝二维数组呢?

2006-03-21 11:15 302 查看
根据微软的文档说明:
Hashtable 元素复制到一维 Array 实例中的指定索引位置。
[C#]
public virtual void CopyTo(
Arrayarray,
intarrayIndex
);

参数

array
一维 Array,它是从 Hashtable 复制的 DictionaryEntry 对象的目标位置。Array 必须具有从零开始的索引。
arrayIndex
array 中的从零开始的索引,从此处开始复制。
但是通过实践能实现二维数组的拷贝,在这里实现二维数组的拷贝:
相关代码如下:
Hashtable ht = new Hashtable();
int[][] a = new int[1][];
a[0] = new int[1]{1};
int[][] b = new int[1][];
b[0] = new int[3]{0,1,2};
int[][] c = new int[2][];
c[0] = new int[3]{1,2,3};
c[1] = new int[2]{3,4};
ht[a[0]] = a[0];
ht[b[0]] = b[0];
ht[c[0]] = c[0];
ht[c[1]] = c[1];
int[][]d = new int[ht.Count][];
ht.Keys.CopyTo(d,0);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: