17

I want to do something like:

object[] rowOfObjects = GetRow();//filled somewhere else
object[,] tableOfObjects = new object[10,10];

tableOfObjects[0] = rowOfObjects;

is this somehow possible and what is the syntax?

or I need to do this:

for (int i = 0; i < rowOfObjects.Length; i++)
{
   tableOfObjects[0,i] = rowOfObjects[i];
}

and fill up the 2 dimensional arrays row using a loop?

Thanks

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
m3ntat
  • 3,635
  • 11
  • 38
  • 50

5 Answers5

11

No, if you are using a two dimensional array it's not possible. You have to copy each item.

If you use a jagged array, it works just fine:

// create array of arrays
object[][] tableOfObject = new object[10][];
// create arrays to put in the array of arrays
for (int i = 0; i < 10; i++) tableOfObject[i] = new object[10];

// get row as array
object[] rowOfObject = GetRow();
// put array in array of arrays
tableOfObjects[0] = rowOfObjects;

If you are getting all the data as rows, you of course don't need the loop that puts arrays in the array of arrays, as you would just replace them anyway.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks so what is the difference between: object[][] tableOfObject = new object[10][10]; and object[,] tableOfObjects = new object[10,10]; Many thanks. – m3ntat Jul 08 '09 at 17:03
  • 3
    An object[,] is a two dimensional array, it is always rectangular (all rows are the same length). An object[][] is a jagged array; an array of object[] arrays. As each row is an array in itself, they don't have to be the same length. – Guffa Jul 08 '09 at 17:16
  • Keep in mind that jagged arrays have poorer performance and occupy more memory than "2d arrays" (they're actually held in memory as a 1d array). Depending on what your program does, this may matter. – Blindy Oct 03 '09 at 10:36
  • @Blindy: You have that *completely* backward. Jagged arrays perform better than 2D arrays. Not sure who told you that, but it isn't true. [Here is a good write up on the topic](http://stackoverflow.com/questions/597720/what-is-differences-between-multidimensional-array-and-array-of-arrays-in-c) – Ed S. Jun 28 '12 at 22:53
  • @Blindy: Even better: http://stackoverflow.com/questions/468832/why-are-multi-dimensional-arrays-in-net-slower-than-normal-arrays – Ed S. Jun 28 '12 at 22:56
  • Why the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer. – Guffa Apr 18 '14 at 18:14
11

If your array is an array of value types, it is possible.

int[,] twoD = new int[2, 2] {
    {0,1},
    {2,3}
};
int[] oneD = new int[2] 
    { 4, 5 };
int destRow = 1;
Buffer.BlockCopy(
    oneD, // src
    0, // srcOffset
    twoD, // dst
    destRow * twoD.GetLength(1) * sizeof(int), // dstOffset
    oneD.Length * sizeof(int)); // count
// twoD now equals
// {0,1},
// {4,5}

It is not possible with an array of objects.

Note: Since .net3.5 this will only work with an array of primitives.

Matthew Finlay
  • 3,354
  • 2
  • 27
  • 32
1

if I have gigabyte size arrays, I would do it in C++/CLI playing with pointers and doing just memcpy instead of having gazillion slow boundary-checked array indexing operations.

0

I find it easiest to just do a loop.

public static double[,] arraycopy(double[] 
thearray, int n, int nrow, int ncol)
{

double[] sourcearray;
double[,] newarray;
int i = 0;
int j = 0;

sourcearray = new double[n];
sourcearray = thearray;
newarray = new double[nrow, ncol];
for(i=0; i<nrow; i++)
{
    for(j=0; j<ncol; j++)
        newarray[i,j] = sourcearray[nrow*i + j]; 
}

return newarray;

}
Gerry
  • 9
  • 2
-1

So, Something like:

    public static object[] GetRow()
    {
        object[,] test = new object[10,10];
        int a = 0;
        object[] row = new object[10];
        for(a = 0; a <= 10; a++)
        {
            row[a] = test[0, a];
        }
        return row;
    }
Justin Drury
  • 748
  • 11
  • 19
  • That's the opposite; getting items from a two dimensional array into a one dimensional array. (Also, an array with the length 10 has index 0-9, not 0-10). – Guffa Jul 08 '09 at 17:20
  • whoops, my bad, I didn't actually run the code, I was just doing it from memory. – Justin Drury Jul 08 '09 at 19:54