简体   繁体   English

C# 拆分多维数组

[英]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.问题:我有一个包含容器的 3 维数组。 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:我能够进行排序的唯一解决方案是制作 2、3 维数组,然后使用 6 个 for 循环来复制大数组:

        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. leftSideOfShip 的 3 个 for 循环按预期工作,但第二个循环给出了 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.其次,您在第二个循环块中有错误:因为您正在循环for (int e = ship.width/2; e < ship.width; e++) e 大于rightSideOfShip数组的第二维。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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