【问题标题】:C# IndexOutOfRangeException when calling Array.GetLength(1) for a 2D Array为 2D 数组调用 Array.GetLength(1) 时出现 C#IndexOutOfRangeException
【发布时间】:2013-11-18 14:45:59
【问题描述】:

这里是代码, 我注意到 Getlength 方法返回 0 但我认为它是正常的,因为 for 循环不会超过 0.. 我已经写了

int yy = tile.GetLength(1); 

它会抛出与下面的代码相同的异常:

 TileData[][] tile = GameMain.Level.getCollisionTiles();

        Rectangle tileRectangle;
        for(int x = 0; x <= tile.GetLength(0); x++)
        {
            for (int y = 0; y <= tile.GetLength(1); y++) //EXCEPTION THROWN HERE AT GETLENGTH !!!!
            {
                tileRectangle = tile[x][y].Target;
                if (tileRectangle.Contains(m_hitbox)) //Si il y a collision
                {
                    if ((m_hitbox.X + m_hitbox.Width) > tileRectangle.X) //si le joueur percute par la gauche
                    {
                        m_hitbox.X--;
                    }
                    else if (m_hitbox.X < (tileRectangle.X + tileRectangle.Width)) //Droite
                    {
                        m_hitbox.X++;
                    }
                    if ((m_hitbox.Y + m_hitbox.Height) > tileRectangle.Y) //si le joueur percute par le haut
                    {
                        m_hitbox.Y--;
                    }
                    else if (m_hitbox.Y < (tileRectangle.Y + tileRectangle.Height)) //Bas
                    {
                        m_hitbox.Y++;
                    }
                }
            }
        }

编辑:我通过从地图对象获取图块信息使其工作,但现在它抛出 NullReferenceException

        TileData[][] tile = GameMain.Level.getCollisionTiles();
        int xMax = GameMain.Level.getMapHeight();
        int yMax = GameMain.Level.getMapWidth();

        for (int x = 0; x <= xMax; x++)
        {
            for (int y = 0; y <= yMax; y++)
            {
                Rectangle tileRectangle = tile[x][y].Target; //THIS LINE FAILS !!!!
                if (tileRectangle.Contains(m_hitbox)) //Si il y a collision
                {
                    if ((m_hitbox.X + m_hitbox.Width) > tileRectangle.X) //si le joueur percute par la gauche
                    {
                        m_hitbox.X--;
                    }
                    else if (m_hitbox.X < (tileRectangle.X + tileRectangle.Width)) //Droite
                    {
                        m_hitbox.X++;
                    }
                    if ((m_hitbox.Y + m_hitbox.Height) > tileRectangle.Y) //si le joueur percute par le haut
                    {
                        m_hitbox.Y--;
                    }
                    else if (m_hitbox.Y < (tileRectangle.Y + tileRectangle.Height)) //Bas
                    {
                        m_hitbox.Y++;
                    }
                }
            }
        }

很抱歉在同一主题中问你这个问题,但我认为你可以帮助我快速解决它。

【问题讨论】:

  • 它不是二维数组。 TileData[][] 不是 TileData[,]

标签: c# arrays multidimensional-array


【解决方案1】:

它不是二维数组。 TileData[][] 是一个锯齿状数组,TileData[,] 是一个二维数组,那么在您的情况下,GetLength(1) 将始终失败,因为 tile 只有一维。

编辑
该怎么做才能解决这个问题?您可以将 tile 从 [][] 更改为 [,](例如)并保持其他所有内容不变(getCollisionTiles() 如何工作?)或者您可以更新代码以从锯齿状数组中获取正确的大小,如下所示:

for (int y = 0; y < tile[x].GetLength(0); y++)

顺便说一句,您甚至可以将GetLength(0) 替换为简单的Length

for(int x = 0; x < tile.Length; x++)
{
    for (int y = 0; y < tile[x].Length; y++)

最后说明:对于基于 0 的数组,长度不包括索引,因此必须将 x &lt;= tile.Length 替换为 x &lt; tile.Length

【讨论】:

  • 感谢您的回答,但是我应该如何获得数组中第二个 [] 的长度?
  • @user3005141 以这种方式初始化你的数组:TileData[,] tile = GameMain.Level.getCollisionTiles();
  • @user3005141 这取决于 getCollisionTiles() 返回的内容...您可以将数组更改为 [,] 而不是 [][] 或者您可以使用 for 来初始化每个元素(请参阅更新的答案) .
  • getCollisionTiles() 返回一个 [][] 数组,但我没有写它,它取决于一个框架,当我尝试转换它时它当然会说: Erreur 3 Unable to convert type 'FuncWorks.XNA.XTiled.TileData[][]' 到 'FuncWorks.XNA.XTiled.TileData[,]'
  • @user3005141 您的第二维没有一个数组。内部维度有 N 个不同的数组,所有数组都可能具有不同的长度。如果你碰巧知道它们都是一样的,你可以选择一个,但如果它们不一样,那么你需要在访问项目时处理这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多