在列表中添加元素会引发ArgumentOutOfRangeException

马泰奥·塞伯纳

我的代码抛出了这个:

ArgumentOutOfRangeException:参数超出范围。
参数名称:索引错误

当它在列表中找到计数少于特定数字的元素时。关于如何更正代码的任何想法?

我在所有地方都添加了debug.log代码来确定确切的错误发生位置,因为Visual Studio不会强调任何错误。

List<int> emptyRows = new List<int>();
for (int j = 0; j < gridPositions.Count; j++) // try to find if a row is still empty
                {
                    Debug.Log("gridPositions[" + j + "].Count is " + gridPositions[j].Count);
                    Debug.Log("columns are" + columns);
                    if (gridPositions[j].Count == columns)
                    {
                        Debug.Log("trying to add to emptyrows");
                        emptyRows.Add(j);
                        Debug.Log("added to emptyrows and its count is " + emptyRows.Count);
                    }
                    else
                    {
                        Debug.Log("found an occupied row at row " + j);
//ERROR STRIKES HERE
                    }
                    Debug.Log("emptyRows is " + emptyRows[j]);
                    Debug.Log("emptyRows count is " + emptyRows.Count);
                }

我希望emptyRows跟踪并记录所有未占用的行,但是当它填充了占用的行时,它不会继续for循环并停止。

非营利组织

如果(gridPositions [j] .Count ==列),则仅添加到emptyRows

但是,您正在访问j的每个值的emptyRows [j]

因此EmptyRows最终的项目较少,则j的值为

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章