How do I smartly initialize an Array with two (or more) other arrays in C#?
double[] d1 = new double[5]; double[] d2 = new double[3]; double[] dTotal = new double[8]; // I need this to be {d1 then d2}Another question: How do I concatenate C# arrays efficiently?
-
4If you have arrays that you need to change or mix and match like this, you should probably be use a generic List instead.Joel Coehoorn– Joel Coehoorn2010-05-07 13:09:36 +00:00Commented May 7, 2010 at 13:09
-
possible duplicate of How do I concatenate two arrays in C#?Matt Sach– Matt Sach2013-08-08 14:37:13 +00:00Commented Aug 8, 2013 at 14:37
Add a comment
|
5 Answers
You could use CopyTo:
double[] d1 = new double[5];
double[] d2 = new double[3];
double[] dTotal = new double[d1.Length + d2.Length];
d1.CopyTo(dTotal, 0);
d2.CopyTo(dTotal, d1.Length);
4 Comments
Julien Hoarau
Msdn is a little bit unclear, but the index parameter specifies the index in the destination array.
Rubys
You need d1.length - 1, I believe
SLaks
You're right; I misunderstood. Sorry. @Rubys: No, you don't.
Julien Hoarau
First I copy 5 doubles from d1 to dTotal. Then I copy d2 to dTotal starting in index 5. If I use
d1.Lenght - 1 I'll start at index 4 and I'll lost the last value of d1.var dTotal = d1.Concat(d2).ToArray();
You could probably make it 'better' by creating dTotal first, and then just copying both inputs with Array.Copy.
2 Comments
SLaks
This will be inefficient for large arrays.
leppie
@SLaks: That's why I added the little extra bit, but even for meduim size arrays (up to 10000 elements), you would probably not even notice the difference. Also Enumerable may provide a fast option for
Concat if both are arrays (will have to look at source to confirm). Update: It does NOT have a fast option for anything.You need to call Array.Copy, like this:
double[] d1 = new double[5];
double[] d2 = new double[3];
double[] dTotal = new double[d1.length + d2.length];
Array.Copy(d1, 0, dTotal, 0, d1.Length);
Array.Copy(d2, 0, dTotal, d1.Length, d2.Length);
Comments
Pretty old post but still actual.. Currently seems to be best solution to use collection expression as follows
var d1 = new double[5];
var d2 = new double[3];
var dTotal = [..d1, ..d2];
// Note: I prefer var on left side
This approach is pretty new, ok, but really short, readable (when you know about it) an effective as much as possible.. Code under the hood seems like this
double[] array = new double[5];
double[] array2 = new double[3];
double[] array3 = array;
double[] array4 = array2;
int num = 0;
double[] array5 = new double[array3.Length + array4.Length];
ReadOnlySpan<double> readOnlySpan = new ReadOnlySpan<double>(array3);
readOnlySpan.CopyTo(new Span<double>(array5).Slice(num, readOnlySpan.Length));
num += readOnlySpan.Length;
ReadOnlySpan<double> readOnlySpan2 = new ReadOnlySpan<double>(array4);
readOnlySpan2.CopyTo(new Span<double>(array5).Slice(num, readOnlySpan2.Length));
num += readOnlySpan2.Length;
and simply allocates final array and uses Spans and Slices to fill it. Awesome!