# Is there a Two-Dimensional Array.Copy Method ??

### Question

• Question:

If I have 2 One-Dimensional Arrays.. I can copy the values from one array to the other in one of the following 2 ways. (ok, yes there are more ways to do it than this, but the other methods are slightly more cumbersome)

Code Snippet

float[] block_ = ...

float[] data    = ...

for ( int i = 0; i < numElements_; ++i )

{

block_[i] = data[i];

}

Or this more preferred method,

Code Snippet
Array
.Copy(data, block_, data.Length);

Now, if I have a Two-Dimensional version of the same above situation:

Code Snippet

float[,] block_ = ...

float[,] data    = ...

for (int i = 0; i < rows_; ++i)

{

for (int j = 0; j < cols_; ++j)

{

block_[i + j * rows_] = data[i, j];

}

}

What would be the equivalent Array.Copy( ?? ); syntax to achieve this?

Wednesday, May 21, 2008 4:34 PM

• Thx Rudedog,

I ran some console experiments and worked out that it is possible for full arrays and this technique will probably work for jagged arrays also, though I have not tried it.

Code Snippet

float[,] f = new float[3,3] { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}  };
float[,] g = new float[3,3];

Array.Copy(f, 0, g, 0, f.Length);

Console.WriteLine("{0} {1} {2}", f[0, 0], f[0, 1], f[0, 2]);
Console.WriteLine("{0} {1} {2}", f[1, 0], f[1, 1], f[1, 2]);
Console.WriteLine("{0} {1} {2}", f[2, 0], f[2, 1], f[2, 2]);
Console.WriteLine();
Console.WriteLine("{0} {1} {2}", g[0, 0], g[0, 1], g[0, 2]);
Console.WriteLine("{0} {1} {2}", g[1, 0], g[1, 1], g[1, 2]);
Console.WriteLine("{0} {1} {2}", g[2, 0], g[2, 1], g[2, 2]);

// 1 2 3
// 4 5 6
// 7 8 9

// 1 2 3
// 4 5 6
// 7 8 9

This should be much faster than using the for(...) method mentioned above.
Wednesday, May 21, 2008 10:20 PM
• Also note that you can use Buffer.BlockCopy() for even greater speed.

For example, you can change the Array.Copy() in the code above to:

Buffer.BlockCopy(f, 0, g, 0, f.Length*sizeof(float));
Thursday, May 22, 2008 9:09 AM
• Thanks Matthew,

I was just tinkering around with the Buffer idea you pointed out and I have found a very neat trick that is not available with Array.Copy() !

I often wish to reshape 2D Arrays into 1D versions which require quite ugly nested loops as Array.Copy() will throw a dimension mismatch if I try and use that in any way.

BUT !!

If you check out the following snippet you can bypass this ugly and cumbersome process by using the Buffer! :-D

Code Snippet

class Program

{

static void Main(string[] args)

{

float[,] f = new float[,] { {1, 2, 3},

{4, 5, 6},

{7, 8, 9} };

float[] g = new float[f.Length];

Buffer.BlockCopy(f, 0, g, 0, f.Length * sizeof(float));

Console.WriteLine("{0} {1} {2}", f[0, 0], f[0, 1], f[0, 2]);

Console.WriteLine("{0} {1} {2}", f[1, 0], f[1, 1], f[1, 2]);

Console.WriteLine("{0} {1} {2}", f[2, 0], f[2, 1], f[2, 2]);

Console.WriteLine();

Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8}",

g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],g[8]);

}

}

// 1 2 3

// 4 5 6

// 7 8 9

// 1 2 3 4 5 6 7 8 9

How cool is that! :-D
Thursday, May 22, 2008 1:36 PM

### All replies

• I believe that Array.Copy() is intended for only one dimensional arrays.  It is up to the developer to create a method for his custom multi-dimensional arrays.  Why?  A multi-dimensional array could also be a jagged array of random sizes.

Rudedog
Wednesday, May 21, 2008 4:50 PM
• Thx Rudedog,

I ran some console experiments and worked out that it is possible for full arrays and this technique will probably work for jagged arrays also, though I have not tried it.

Code Snippet

float[,] f = new float[3,3] { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}  };
float[,] g = new float[3,3];

Array.Copy(f, 0, g, 0, f.Length);

Console.WriteLine("{0} {1} {2}", f[0, 0], f[0, 1], f[0, 2]);
Console.WriteLine("{0} {1} {2}", f[1, 0], f[1, 1], f[1, 2]);
Console.WriteLine("{0} {1} {2}", f[2, 0], f[2, 1], f[2, 2]);
Console.WriteLine();
Console.WriteLine("{0} {1} {2}", g[0, 0], g[0, 1], g[0, 2]);
Console.WriteLine("{0} {1} {2}", g[1, 0], g[1, 1], g[1, 2]);
Console.WriteLine("{0} {1} {2}", g[2, 0], g[2, 1], g[2, 2]);

// 1 2 3
// 4 5 6
// 7 8 9

// 1 2 3
// 4 5 6
// 7 8 9

This should be much faster than using the for(...) method mentioned above.
Wednesday, May 21, 2008 10:20 PM
• Also note that you can use Buffer.BlockCopy() for even greater speed.

For example, you can change the Array.Copy() in the code above to:

Buffer.BlockCopy(f, 0, g, 0, f.Length*sizeof(float));
Thursday, May 22, 2008 9:09 AM
• Thanks Matthew,

I was just tinkering around with the Buffer idea you pointed out and I have found a very neat trick that is not available with Array.Copy() !

I often wish to reshape 2D Arrays into 1D versions which require quite ugly nested loops as Array.Copy() will throw a dimension mismatch if I try and use that in any way.

BUT !!

If you check out the following snippet you can bypass this ugly and cumbersome process by using the Buffer! :-D

Code Snippet

class Program

{

static void Main(string[] args)

{

float[,] f = new float[,] { {1, 2, 3},

{4, 5, 6},

{7, 8, 9} };

float[] g = new float[f.Length];

Buffer.BlockCopy(f, 0, g, 0, f.Length * sizeof(float));

Console.WriteLine("{0} {1} {2}", f[0, 0], f[0, 1], f[0, 2]);

Console.WriteLine("{0} {1} {2}", f[1, 0], f[1, 1], f[1, 2]);

Console.WriteLine("{0} {1} {2}", f[2, 0], f[2, 1], f[2, 2]);

Console.WriteLine();

Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8}",

g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],g[8]);

}

}

// 1 2 3

// 4 5 6

// 7 8 9

// 1 2 3 4 5 6 7 8 9

How cool is that! :-D
Thursday, May 22, 2008 1:36 PM