【问题标题】:C# Multidimensional Array Of Rectangles?C#多维矩形数组?
【发布时间】:2016-03-12 13:37:22
【问题描述】:

我用这个矩形阵列碰壁了。简单的一维数组我很好,但是一个有 4 列的数组我刚刚开始把头撞在墙上......

我已将数组定义为:

private Rectangle[,] brick = new Rectangle[2, 8];

然后将这些与那个结合使用:

int[,] brickLocation = { {0, 0}, {0,21}, {0,42}, {0, 63}, {0, 84}, {0, 105}, {0, 126},
                             {61, 0}, {61,21}, {61,42}, {61, 63}, {61, 84}, {61, 105}, {61, 126} };
bool[] brickLive = { true, true, true, true, true, true, true,
                       true, true, true, true, true, true, true };

然后尝试通过多数组循环绘制矩形:

for (int i = 0; i < brickLive.Length; i++)
{
    for (int j = 0; j < brickLive.Length; i++)
    {
        if (brickLive[i] == true)
        {
            brick[i, j] = new Rectangle(brickLocation[i, 0], brickLocation[i, 1], brkLength, brkHeight);
                    brickPaper.DrawRectangle(brickPen, brick[i, j]);
        }
        else
        {
            continue; //move onto next brick
        }
    }
}

它停止工作了,我想不出我能做什么....有人可以帮忙吗?

【问题讨论】:

  • 它究竟是如何“停止工作”的?是否有一些错误消息或行为是否意外?
  • 我得到了以下行的 IndexOutOfRangeException:brick[i, j] = new Rectangle(brickLocation[i, 0],brickLocation[i, 1], brkLength, brkHeight);
  • 你的 j for 循环中有 i++
  • 哦,我做到了,但还是不行。

标签: c# arrays multidimensional-array


【解决方案1】:

据我了解,在行中

for (int i = 0; i < brickLive.Length; i++)
for (int j = 0; j < brickLive.Length; i++)

对于第二行中的循环,您的意思是增加j,但您却增加了i

【讨论】:

  • 他也两次使用长度属性,这也不正确。
  • 是的,应该是28,用于创建数组。
  • 第二次用什么代替length()?
  • GetLength(0) 和 GetLength(1)
  • 访问长度在this question中描述
猜你喜欢
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2012-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
相关资源
最近更新 更多