Use the Array.Copy method to copy values in two-dimensional arrays in C#

The Array.Copy method can copy values from one array to another. For one-dimensional arrays, this is reasonably straightforward. You specify the source and destination arrays, the indices where copying should start in the two arrays, and the number of items to copy. The process is the same for two-dimensional arrays, it's just harder to visualize the result.

The items in a two-dimensional array are stored in a contiguous piece of memory in row-major order. That means the first row comes first, then the second row, then the third, and so forth. In memory that means the last item in a row is immediately followed by the first item in the following row.

When you use Array.Copy to copy two-dimensional arrays, the arrays are treated as if they were one-dimensional arrays with the values packed into it in row-major order. Instead of specifying the row and column in the array where the copying should start, you specify the index in the one-dimensional version. If you know the row and column where you want copying to start, it's easy enough to calculate this index.

For example, if an array has num_cols columns and you want copying to start at row r and column c, then the "one-dimensional" index is:

    index = r * num_cols + c

You can use this equation to calculate the index in the source array where copying should begin and to calculate the index in the destination array where the values should be copied. Remember that the two arrays may have different sizes, however, so the value num_cols may be different. (Notice that the number of rows doesn't matter.)

When you call Array.Copy passing it these indices, if the values extends past the end of a row, they simply wrap around to the next row (because the values are packed in memory in row-major order).

This example uses the following code to copy values from one array to another.

// Copy the values and display the result.
private void btnCopy2d_Click(object sender, EventArgs e)
{
    string[,] new_values = new string[3, 3];
    for (int row = 0; row <= new_values.GetUpperBound(0); row++)
    {
        for (int col = 0; col <= new_values.GetUpperBound(1); col++)
        {
            new_values[row, col] = "------";
        }
    }

    Array.Copy(Values2d, 6, new_values, 2, 6);
    ShowValues(lstCopy, new_values);
}

The form's Load event, which isn't shown here, initializes the Values2d array. This code creates a new array called new_values with 3 rows and 3 columns. (Notice that this doesn't match the Values2d array, which has 5 rows and 4 columns.) The code then fills each entry in the new array with the string "------."

Next the code calls Array.Copy to copy values from Values2d into new_values. It starts taking values from index 6 in the Values2d array. If you use the earlier equation or just count entries in the picture (starting at 0, as usual), you'll see that index 6 corresponds to row 1 column 2 where the entry is (1, 2).

The values are copies into new_values at index 2, which corresponds to row 0 column 2.

The code copies 6 items. Notice in the picture how the values from both arrays wrap from one row to another. (I added the red rectangles to show which values are copied. The rectangles are not part of the program's output.

The Array.Copy method can copy values between two-dimensional arrays but it is confusing. The next post will describe some extension methods that make it easier to copy between arrays in general and two-dimensional arrays in particular.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.