How to merge two arrays as one

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I have two array like
string a1= {"dog","dock","deer"}
string a2= {"lion","tiger"}

How can I merge a1 and a2 to a3
a3: {"dog","dock","deer", "lion","tiger"}
 
ad said:
I have two array like
string a1= {"dog","dock","deer"}
string a2= {"lion","tiger"}

How can I merge a1 and a2 to a3
a3: {"dog","dock","deer", "lion","tiger"}

Well, for starters I assume that the declarations are, more properly:

string[] a1 = { "dog", "dock", "deer" };
string[] a2 = { "lion", "tiger" };

then you can append them using:

string[] a3 = new string[a1.Length + a2.Length];
a1.CopyTo(a3, 0);
a2.CopyTo(a3, a1.Length);
 
I have two array like
string a1= {"dog","dock","deer"}
string a2= {"lion","tiger"}

How can I merge a1 and a2 to a3
a3: {"dog","dock","deer", "lion","tiger"}

Create a new array long enough to contain all elements from both
arrays. Copy over the elements from the old arrays to the new (using
Array.Copy, Array.CopyTo or manually looping).


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top