2009-07-08 11 views
12

quiero hacer algo como:C# 1 asignan matriz bidimensional a la sintaxis 2 matriz bidimensional

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

tableOfObjects[0] = rowOfObjects; 

es esto de alguna manera posible y lo que es la sintaxis?

o tengo que hacer esto:

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

y llenar la fila 2 matrices tridimensionales utilizando un bucle?

Gracias

Respuesta

7

No, si está utilizando una matriz de dos dimensiones que no es posible. Tienes que copiar cada elemento.

Si se utiliza una matriz escalonada, funciona muy bien:

// 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; 

Si usted está recibiendo todos los datos como filas, que por supuesto no es necesario el bucle que pone matrices en la matriz de matrices , ya que simplemente los reemplazarías de todos modos.

+0

Gracias, ¿cuál es la diferencia entre: objeto [] [] tableOfObject = new object [10] [10]; y objeto [,] tableOfObjects = new object [10,10]; Muchas gracias. – m3ntat

+3

Un objeto [,] es una matriz bidimensional, siempre es rectangular (todas las filas tienen la misma longitud). Un objeto [] [] es una matriz dentada; una matriz de matrices de objetos []. Como cada fila es una matriz en sí misma, no es necesario que tengan la misma longitud. – Guffa

+0

Ahh eso tiene sentido. Gracias Guffa. – m3ntat

-1

Por lo tanto, algo así como:

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; 
    } 
+0

Eso es todo lo contrario; obtener elementos de una matriz bidimensional en una matriz unidimensional. (Además, una matriz con la longitud 10 tiene índice 0-9, no 0-10). – Guffa

+0

whoops, mi mal, en realidad no ejecuté el código, solo lo estaba haciendo desde la memoria. –

1

si tengo matrices de tamaño gigabyte, lo haría en C++/CLI jugar con los punteros y hacer precisamente memcpy en lugar de tener lentas operaciones de indización de matrices de frontera a cuadros tropecientos.

8

Si su matriz es una matriz de value types, es posible.

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} 

No es posible con una matriz de objetos.

Nota: Desde .net3.5 esto solo funcionará con una matriz de primitivas.

Cuestiones relacionadas