将两个数组合并为一个数组 [英] Merging two arrays into one array

查看:174
本文介绍了将两个数组合并为一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以下面给出的特定顺序合并两个数组,



示例:

------ ----



string [] array1 = {1,2,3};

string [] array2 = {6,7 ,8};

我想要这个输出 - > string [] MergedArray = {1,6,2,7,3,8};



与上面的例子一样,我想将第一个数组的第一个元素添加到第二个数组的第一个元素。如果有可能请给我代码....

解决方案

试试这个



  int  [] array1 = { 1  2  3  4  5 }; 
int [] array2 = { 6 7 8 9 10 };
var list = new List< int>();
for int i = 0 ; i < array1.Length; i ++)
{
list.Add(array1 [i]);
list.Add(array2 [i]);
}
int [] array3 = list.ToArray();


< blockquote>没有直接方法这样做....因为你需要将第一个数组的第一个元素与第二个数组的第一个元素合并然后第一个数组的第二个元素第二个数组的第二个元素。



你应该使用循环。



希望它有所帮助:)


看看这里: http://www.authorcode.com/how-to-merge-two-or-more-array-in-single-array-in-c/ [ ^ ]


I want to merge two arrays in the this particular order is given below,

Example:
----------

string[] array1 = { 1, 2, 3};
string[] array2 = { 6, 7, 8};
I want this output -> string[] MergedArray={1,6,2,7,3,8};

Like the above example i want to add the first array's first element to the second array's first element.If it is possible kindly give me the code....

解决方案

Try this

int[] array1 = { 1, 2, 3, 4, 5 };
           int[] array2 = { 6, 7, 8, 9, 10 };
           var list = new List<int>();
           for (int i = 0; i < array1.Length; i++)
           {
               list.Add(array1[i]);
               list.Add(array2[i]);
           }
           int[] array3 = list.ToArray();


No direct method to do so....as you need to merge the first element of the first array with the first element of the second array then second element of the first array to the second element of the second array.

You should use loop for this.

Hope it helps :)


Have a look here: http://www.authorcode.com/how-to-merge-two-or-more-array-in-single-array-in-c/[^]


这篇关于将两个数组合并为一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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