Forum Moderators: open

Message Too Old, No Replies

C# and arrays...

I need to join some arrays, how?

         

joshie76

2:26 pm on Dec 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can I join a number of separate arrays using C# in .NET?

When I say join I mean take, for example, 2 arrays with 4 entries in each and combine them to become 1 array with 8 entries? Order is unimportant.

Thanks

J

joshie76

10:35 am on Dec 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, in the end I wrote a function to do it though I'm sure there must be a better and faster way...

private string[] combineArrays(string[] arr1, string[] arr2) 
{
string[] combArray = new string[arr1.Length + arr2.Length];
for (int i=0; i < arr1.Length; i++)
{
combArray[i] = arr1[i];
}
for (int i=0; i < arr2.Length; i++)
{
combArray[arr1.Length+i] = arr2[i];
}
return combArray;
}

joshie76

4:43 pm on Dec 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, getting there now!

The CopyTo method seems to be much quicker than for loop and manually rebuilding the array... now if I could just find a way to redimension the arr1 so that the first CopyTo is not needed....

private string[] combineArrays(string[] arr1, string[] arr2) 
{
string[] combArray = new string[arr1.Length + arr2.Length];
arr1.CopyTo(combArray,0);
arr2.CopyTo(combArray,arr1.Length);
return combArray;
}