I am attempting to resize a 3D array in C#.
The VB6 code reads:
ReDim Combos(ProductNum, 4, 3)
Since I cannot use Array.Resize
for a 3D array, is there another way for me to do this function in C#?
I have already declared the array as a 3D array:
int[ , , ] Combos;
But I am having trouble resizing it. Any ideas?
The shape of the array can also be changed using the resize() method. If the specified dimension is larger than the actual array, The extra spaces in the new array will be filled with repeated copies of the original array.
To resize images in Python using OpenCV, use cv2. resize() method. OpenCV provides us number of interpolation methods to resize the image. Resizing the image means changing the dimensions of it.
With the help of Numpy numpy. resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing.
reshape() and numpy. resize() methods are used to change the size of a NumPy array. The difference between them is that the reshape() does not changes the original array but only returns the changed array, whereas the resize() method returns nothing and directly changes the original array.
There is no way to directly redimension a multidimensional array in .NET. I would recommend allocating a new array, and then copying the values into it using Array.Copy
.
That being said, depending on what you're doing, you may want to consider using a different collection type, as well. There are more options in .NET than VB6 in terms of collections, so many cases which used multidimensional arrays are better suited towards other collection types, especially if they are going to be resized.
Even for the one-dimensional array, in C#, the array resizing works by copying elements into new re-sized array.
If you need to re-size array often, collections would be much better solution.
If you still want to resize array, here is the code:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With