从特定索引复制数组

时间:2010-10-27 18:43:26

标签: c# arrays

如何在不循环的情况下从特定索引开始将值数组复制到目标数组?

例如,如果我有一个包含2个值的数组,我必须将这两个元素复制到另一个从索引3开始容量为5的数组?

 double[] source = new double[] {1, 2};
 double[] destination = new double[5]{0,0,0,0,0};
 //How to perform this copy?
 double[] result = new double[5] {0, 0, 0, 1, 2};

3 个答案:

答案 0 :(得分:8)

这是你要找的吗?

Array.Copy(source, 0 /*start loc*/, destination, 3 /*start loc*/, 2 /*count*/);

答案 1 :(得分:7)

使用Array.CopyTo或静态方法Array.Copy

source.CopyTo(destination, 3);

答案 2 :(得分:0)

double[] source = new double[] {1, 2};
 double[] destination = new double[5]{0,0,0,0,0};
 //How to perform this copy?
ArrayList result = new ArrayList();
result.AddRange(source);
result.AddRange(destination);
destination = result.ToArray();