BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.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 three steps. Firstly, a new array is created with a size equal to the total length of the arrays being processed. This 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.

The second step is to copy the contents of the first source array into the destination array, starting at the first index. 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 use the static Array.Copy method, which is found in the System namespace. In this article we will use two overloaded versions of this method.

Combining Two Arrays

In the remainder of this article we will create a simple program that generates two arrays, combines them into a single list and outputs the new array's 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 begin, create a console application and add the following code to the Main method. This creates two source arrays containing strings.

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.

string[] shoppingList = new string[7];

Copying the First Array

The first source array is copied to the beginning of the destination. For this we can use the simplest version of the Array.Copy method, which 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 array. This will be the index immediately following the copied contents from the first source array. To specify this we must use an overloaded version of Array.Copy that adds two further 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 target index for copying. As we have already copied three items into this array at indices 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 source 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

*/
30 May 2009