Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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....
Posted

Try this

C#
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();
 
Share this answer
 
Comments
Sanket Saxena 7-May-14 7:56am    
hey this is what i was talking about in my solution. Using loop.

+5
Manivignesh 7-May-14 7:58am    
Thank you so much friend..this is what i want...thaks a lot.. :-)
Maciej Los 7-May-14 8:49am    
Have you seen my answer (solution 2)? There are two methods. Follow the link provided.
King Fisher 7-May-14 8:56am    
Welcome :)
Maciej Los 7-May-14 8:53am    
+5!
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 :)
 
Share this answer
 
v2
Comments
DamithSL 7-May-14 7:52am    
No need to loop if you know LINQ
Sanket Saxena 7-May-14 7:54am    
yes. we can avoid loop if use LINQ.
Maciej Los 7-May-14 8:50am    
Well... Not true! Using loop = ineffective way!
Maciej Los 7-May-14 9:26am    
Thank you for clarification (in the comment to my answer). In this case, you're right. +5!
Sanket Saxena 7-May-14 9:33am    
Great thanks Maciej :)
 
Share this answer
 
Comments
CHill60 7-May-14 9:05am    
Not sure why this was downvoted - shows two different methods in a simple way. 5'd for balance
Maciej Los 7-May-14 9:10am    
Thank you, CHill60 ;)<br>
I suspect the reason: samples doesn't provide direct code(way) to achieve what OP wants to achieve.
Sanket Saxena 7-May-14 9:16am    
Actually i am not sure with your solution because it is not satisfying the OP's requirement thats why didnt comment on your solution even not downvoted.
Maciej Los 7-May-14 9:25am    
Thank you for clarification.
Sanket Saxena 7-May-14 9:33am    
thanks and always welcome :)
It is possible with one line of LINQ Code :)
C#
string[] array1 = new string[] { "1", "2", "3"};
string[] array2 = new string[] { "6", "7", "8"};

string[] MergedArray = array1.Zip(array2, (a,b)=> new[]{a,b}).SelectMany(x=>x).ToArray();
 
Share this answer
 
Comments
Sanket Saxena 7-May-14 7:53am    
+5 for using LINQ
DamithSL 7-May-14 7:59am    
thanks Sanket
Sanket Saxena 7-May-14 8:00am    
most welcome :)
Vi(ky 7-May-14 7:58am    
+5 :)
DamithSL 7-May-14 7:59am    
Thanks Ravi
C#
int [] SouceArray1 = new int[] {2,1,3};
int [] SourceArray2 = new int[] {4,5,6};
int [] targetArray = new int [SouceArray1.Length + SourceArray2.Length];
SouceArray1.CopyTo(targetArray,0);
SourceArray2.CopyTo(targetArray,SouceArray1.Length) ;
foreach (int i in targetArray) Console.WriteLine(i + " ");
 
Share this answer
 
Comments
Sanket Saxena 7-May-14 7:30am    
it will just merge the array but not serves the purpose of the OP. its requirement is different.
Manivignesh 7-May-14 7:49am    
Thanks for the code...but this code helps to add two arrays one by one..but i need to add the first index element of first array with the second index element of the second array...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900