BlackWasp
.NET Framework
.NET 1.1+

Combining Arrays

A common requirement when developing software is to combine several arrays. This article explains the process of copying two arrays into a third, larger structure. This process can be extended to join together the contents of any number of arrays.

Array.Copy

The process of combining two arrays is quite simple, involving only three stages. Firstly, a new array must be created with a size equal to the total of the sizes of the arrays being processed. This array will hold the combined contents of the source arrays and is required because it is not possible to change the size of either of the source arrays once set. The second step is to copy the contents of the first source array into the destination array, starting at the first index. Finally, step three is to copy the second source array into the destination array, starting at the index immediately after the items already copied.

To copy elements from one array into another you can use the static Array.Copy method, which is found in the System namespace. In this article we will use two overloaded variations of this method.

Combining Two Arrays

In the remainder of this article we will create a simple program that generates two arrays and combines them into a single set of values. We will then output the values to the console. If you wish to combine more than two arrays, ensure that you create a destination array that is large enough to contain the contents of all of the source arrays, then use the Array.Copy several times to complete the operation.

To begin, create a console application and add the following code to the Main method. This creates two arrays containing strings. These will be the source arrays for the example.

string[] fruit = new string[] { "Apple", "Orange", "Banana" };
string[] veg = new string[] { "Cabbage", "Lettuce", "Carrot", "Peas" };

Creating the Destination Array

As the combined size of the two arrays is seven items, we now need to create a new string array with this length. This will be the destination array that contains all seven items once the process is completed.

string[] shoppingList = new string[7];

Copying the First Array

The first source array will be copied to the beginning of the destination. For this we can use the simplest version of the Array.Copy method. This requires three parameters. The first parameter is the array to be copied. The second is the destination array and the third is the number of elements to copy. As we wish to duplicate the entire source array, the third parameter should contain the size of that array.

To copy all three elements of the fruit array into the shoppingList array, add the following code:

Array.Copy(fruit, shoppingList, 3);

Copying the Second Array

The second source array must be copied to a specific location in the destination. This will be the index immediately following the copied contents from the first source array. To specify this we must use a different overloaded version of Array.Copy. This version adds another two parameters. The first and second parameters now specify the source array and the first index of that array to be copied. In this case the source array is held in the veg variable and we wish to copy from index zero.

The third and fourth parameters specify the destination array and the first index that should be used for copying. As we have already copied three items into this array at indexes 0, 1 and 2, the next copy should be at index 3. Finally, the fifth parameter specifies the number of elements to copy. Again we want the entire array, which contains four strings.

Array.Copy(veg, 0, shoppingList, 3, 4);

Testing the Code

To test that the combining operation works correctly add the following code to the Main method. This loops through the items in the destination array and outputs them to the console. Run the program to see the results.

foreach (string item in shoppingList)
{
    Console.WriteLine(item);
}

/* OUTPUT

Apple
Orange
Banana
Cabbage
Lettuce
Carrot
Peas

*/
Link to this Page30 May 2009
TwitterTwitter RSS Feed RSS