Resize 3D Arrays

I am trying to resize a 3D array in C #.

VB6 code reads:

ReDim Combos(ProductNum, 4, 3)

Since I cannot use Array.Resizefor a 3D array, is there any other way to execute this function in C #?

I already declared the array as a three-dimensional array:

int[ , , ] Combos;

But I have problems resizing. Any ideas?

+5
source share
3 answers

It is not possible to directly reassign a multidimensional array in .NET. I would recommend allocating a new array and then copying the values ​​into it using Array.Copy.

, , , . .NET , VB6 , , , , .

+4

, #, .

, .

, :

T[,,] ResizeArray<T>(T[,,] original, int xSize, int ySize, int zSize)
{
    var newArray = new T[xSize, ySize, zSize];
    var xMin = Math.Min(xSize, original.GetLength(0));
    var yMin = Math.Min(ySize, original.GetLength(1));
    var zMin = Math.Min(zSize, original.GetLength(2));
    for (var x = 0; x < xMin; x++)
        for (var y = 0; y < yMin; y++)
            for (var z = 0; z < zMin; z++)
                newArray[x, y, z] = original[x, y, z];
    return newArray;
}
+4

:

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

: http://www.codeproject.com/Articles/2503/Redhotglue-C-ArrayObject

# (VS 2008) :

    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();
    }

, .

+1

All Articles