Copy and Merge 2 Arrays

VBOfficer

Newcomer
Joined
Jan 22, 2009
Messages
24
Hi,
I have 2 arrays, I don't know their length...
I wanna copy both arrays into 1 single array.
So the length of output array will be sum of my 2 input arrays.
I know I should use Array.Copy but don't know how, for my case, it won't merge the contents of 2 arrays into 1.
Anyone can help me?

I also used this but won't work:
Visual Basic:
ReDim OutputArray(InputArray1.Length + InputArray2.Length - 1)
Array.Copy(InputArray1, OutputArray, InputArray1.Length)
Array.Copy(InputArray2, OutputArray, InputArray2.Length)

Thanks :)
 
Last edited:
You need to specify where to begin copying to when calling Array.Copy using a different overload (at least with InputArray2, since you don't want to copy that to the beginning of OutputArray).
Visual Basic:
ReDim OutputArray(InputArray1.Length + InputArray2.Length - 1)
        ' Parameters are sourceArray, sourceOffset, destArray, destOffset, length)
        Array.Copy(InputArray1, 0, OutputArray, 0, InputArray1.Length)
        Array.Copy(InputArray2, 0, OutputArray, InputArray1.Length, InputArray2.Length)
In the last line, the code specifies to copy InputArray2 starting where InputArray1 left off (the second to last parameter).
 
Back
Top