简体   繁体   中英

C# Splitting a multidimensional array

So I have been searching for an answer for this for about an hour now and I was unable to find one so I'm going to try my luck here.

The problem: I have a 3 dimensional Array that contains Containers. This array is for an algoritm for placing containers on a cargoship. The array consists of Length, width and height. I am trying to split the array at the middle of the width.

The only solution I have been able to sort of make work is making 2, 3 dimensional arrays and then using 6 for loops to copy the big array:

        Container[,,] leftSideOfShip = new Container[ship.length, ((ship.width) / 2), ship.height];
        Container[,,] rightSideOfShip = new Container[ship.length, ((ship.width) / 2), ship.height];
        for(int a = 0; a < ship.length; a++)
        {
            for(int b = 0; b < ship.width/2; b++)
            {
                for(int c = 0; c < ship.height; c++)
                {
                    if(ship.position[a,b,c] != null)
                    {
                        leftSideOfShip[a, b, c] = ship.position[a, b, c];
                    }
                }
            }
        }
        for (int d = 0; d < ship.length; d++)
        {
            for (int e = ship.width/2; e < ship.width; e++)
            {
                for (int f = 0; f < ship.height; f++)
                {
                    if(ship.position[d,e,f] != null)
                    {
                        rightSideOfShip[d, e, f] = ship.position[d, e, f];
                    }
                }
            }
        }

The 3 for loops for the leftSideOfShip work as expected but the second one gives an indexOutOfRangeException.

Is there a better way to split this and if so how?

First of all, you need to be mindful of how you round side width in case the width of the ship is odd. For example, you can always assume, that the left side is wider in case the width of the ship is odd, something like that:

int leftSideWidth = ship.width % 2 == 0 ? ship.width / 2 : (ship.width / 2) + 1;
int rightSideWidth = ship.width / 2;

Second of all, you have an error in your second loop block: since you are looping for (int e = ship.width/2; e < ship.width; e++) e is greater than second dimension of rightSideOfShip array.

Full solution will look something like this:

Container[,,] leftSideOfShip = new Container[ship.length, leftSideWidth, ship.height];
Container[,,] rightSideOfShip = new Container[ship.length, rightSideWidth, ship.height];

for (int i = 0; i < ship.length; i++)
{
    for (int j = 0; j < ship.width; j++)
    {
        for (int k = 0; k < ship.height; k++)
        {
            if (j < leftSideWidth)
            {
                leftSideOfShip[i, j, k] = ship.position[i, j, k];
            }
            else
            {
                rightSideOfShip[i, j - leftSideWidth, k] = ship.position[i, j, k];
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM