简体   繁体   English

从特定索引复制数组

[英]Copy arrays from specific indexes

How can I copy an array of values to a destination array starting from a specific index without looping? 如何在不循环的情况下从特定索引开始将值数组复制到目标数组?

For example,if I have an array with 2 values, I have to copy those two elements to another array which has a capacity of 5 starting from index 3? 例如,如果我有一个包含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};

这是你在找什么?

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

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

source.CopyTo(destination, 3);
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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM