views:

2600

answers:

6

Is there a built in function in .Net 2.0 that will take two arrays and merge them into one array? The arrays are both of the same type. I'm getting these arrays from a widely used function within my code base and can't modify the function to return the data in a different format.

I'm looking to avoid writing my own function to accomplish this if possible.

+4  A: 

I think you can use Array.Copy for this. It takes a source index and destination index so you should be able to append the one array to the other. If you need to go more complex than just appending one to the other, this may not be the right tool for you.

Geoffrey Chetwood
+1  A: 

Assuming the destination array has enough space, Array.Copy() will work. You might also try using a List and it's .AddRange() method.

Joel Coehoorn
A: 

I'm assuming you're using your own array types as opposed to the built-in .NET arrays:


public string[] merge(input1, input2){
  string[] output = new string[input1.length + input2.length];
  for(int i = 0; i = input1.length)
      {
          output[i] = input2[i-input1.length];
      }
      else{
          output[i] = input1[i];
      }
   }
   return output;
}

NOTE: Due to an error with the <pre> tag, the if (i >= input1.length) doesn't show up. It should be above the output[i] = input2[i-input1.length]
Another way of doing this would be using the built in ArrayList class.

public ArrayList merge(input1, input2){
   Arraylist output = new ArrayList();
   foreach(string val in input1){
      output.add(val);
   }
   foreach(string val in input2){
      output.add(val);
   }
return output;
}

Both examples are C#.

apandit
+6  A: 

If you can manipulate one of the arrays, you can resize it before performing the copy:

T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
int array1OriginalLength = array1.Length;
Array<T>.Resize(array1, array1OriginalLength + array2.Length);
Array.Copy(array2, 0, array1, array1OriginalLength, array2.Length);

Otherwise, you can make a new array

T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
T[] newArray = new T[array1.Length + array2.Length];
Array.Copy(array1, 0, newArray);
Array.Copy(array2, 0, newArray, array1.Length, array2.Length);

More on available Array methods on MSDN.

Blair Conrad
Just realized, you have to save the length of the first array in a separate variable before resizing.
kbrinley
Right! Foolish of me. That's what I get for supplying code I've not run...
Blair Conrad
What about .NET 4.0, any news?
Shimmy
+2  A: 

First, make sure you ask yourself the question "Should I really be using an Array here"?

Unless you're building something where speed is of the utmost importance, a typed List, like List is probably the way to go. The only time I ever use arrays are for byte arrays when sending stuff over the network. Other than that, I never touch them.

+17  A: 
OwenP
I like that LINQ implementation. I really need to make the jump and get into LINQ soon...
Geoffrey Chetwood
Rich, the best part about the LINQ implementation is not only is it concise, it's also just as efficient as the 2.0 version, since it works against IEnumerable.
Brad Wilson
+1, .Concat really helped me out.
tsilb
this solution should be voted the correct answer i think
RoboShop