Código:
public int[,,] ReDimension(int[,,] OldArray,int arr1stDimLength,int arr2ndDimLength,int arr3rdDimLength)
{
// declare a larger array
int[,,] NewArray = new int[arr1stDimLength,arr2ndDimLength,arr3rdDimLength];
// determine if we are shrinking or enlarging
const int FirstDimension = 0;
const int SecondDimension = 1;
const int ThirdDimension = 2;
int xMax = 0;
int yMax = 0;
int zMax = 0;
// determine if we are shrinking or enlarging columns
if (OldArray.GetUpperBound(FirstDimension) < (arr1stDimLength - 1))
xMax = OldArray.GetUpperBound(FirstDimension) + 1;
else
xMax = arr1stDimLength;
// determine if we are shrinking or enlarging rows
if (OldArray.GetUpperBound(SecondDimension) < (arr2ndDimLength - 1))
yMax = OldArray.GetUpperBound(SecondDimension) + 1;
else
yMax = arr2ndDimLength;
// determine if we are shrinking or enlarging depth
if (OldArray.GetUpperBound(ThirdDimension) < (arr3rdDimLength - 1))
zMax = OldArray.GetUpperBound(ThirdDimension) + 1;
else
zMax = arr3rdDimLength;
// place values of old array into new array
for(int z = 0; z < zMax; z++)
{
for(int x = 0; x < xMax; x++)
{
for(int y = 0; y < yMax; y++)
{
NewArray[x, y, z] = OldArray[x, y, z];
Console.Write("[{0}]", NewArray[x, y, z]);
}
Console.Write("\n");
}
Console.Write("\n\n");
}
return NewArray;
}
Tomado de: http://www.codeproject.com/Articles/2503/Redhotglue-C-ArrayObject
Probado en C# (VS 2008) con una aplicación de consola:
static void Main(string[] args)
{
int[, ,] combos = new int[1,2,3];
Console.WriteLine("Combos size: {0}",combos.Length.ToString());
combos = ReDimension(combos, 5, 5, 5);
Console.WriteLine("New Combos size: {0}", combos.Length.ToString());
Console.ReadKey();
}
Parece que funciona bien.
He intentado usar Array.Copy pero no estaba seguro de cómo funcionaría. –
ver http://stackoverflow.com/questions/6539571/how-to-resize-multidimensional-2d-array-in-c – ken2k
¿Hay alguna razón por la que está usando una matriz en lugar de, por ejemplo, la lista? –
RobIII