方形拼图 C#
你能帮助我并纠正我的代码吗
static void SolveAndDraw(int number)
{
// Create Dynamic List of list to
List<List<int>> matrix = new List<List<int>>();
// Intialize the inner lists
for (int j = 0; j < number; j++)
{
matrix.Add(new List<int>());
}
char direction = 'r';
int xPos = 0, yPos = 0;
int rightLimit = number - 1;
int leftLimit = 0;
int upLimit = 0;
int bottomLimit = number - 1;
for (int i = 1; i <= number * number; ++i)
{
// matrix[yPos][xPos] = i;
matrix[xPos].Insert(yPos, i);
switch (direction)
{
case 'r':
if (xPos < rightLimit)
{
++xPos;
}
else
{
direction = 'd';
++upLimit;
++yPos;
}
break;
case 'l':
if (xPos > leftLimit)
{
--xPos;
}
else
{
direction = 'u';
--bottomLimit;
--yPos;
}
break;
case 'u':
if (yPos > upLimit)
{
--yPos;
}
else
{
direction = 'r';
++leftLimit;
++xPos;
}
break;
case 'd':
if (yPos < bottomLimit)
{
++yPos;
}
else
{
direction = 'l';
--rightLimit;
--xPos;
}
break;
}
}
// Now, just dump the matrix contents to stdout
for (int i = 0; i < number; ++i)
{
for (int j = 0; j < number; ++j)
{
Console.Write("{0}\t", matrix[i][j]);
}
Console.Write("\n");
}
Console.ReadLine();
}
它崩溃并给出错误:
索引必须在列表的范围内。
参数名称:索引
Could u help me and correct my code
static void SolveAndDraw(int number)
{
// Create Dynamic List of list to
List<List<int>> matrix = new List<List<int>>();
// Intialize the inner lists
for (int j = 0; j < number; j++)
{
matrix.Add(new List<int>());
}
char direction = 'r';
int xPos = 0, yPos = 0;
int rightLimit = number - 1;
int leftLimit = 0;
int upLimit = 0;
int bottomLimit = number - 1;
for (int i = 1; i <= number * number; ++i)
{
// matrix[yPos][xPos] = i;
matrix[xPos].Insert(yPos, i);
switch (direction)
{
case 'r':
if (xPos < rightLimit)
{
++xPos;
}
else
{
direction = 'd';
++upLimit;
++yPos;
}
break;
case 'l':
if (xPos > leftLimit)
{
--xPos;
}
else
{
direction = 'u';
--bottomLimit;
--yPos;
}
break;
case 'u':
if (yPos > upLimit)
{
--yPos;
}
else
{
direction = 'r';
++leftLimit;
++xPos;
}
break;
case 'd':
if (yPos < bottomLimit)
{
++yPos;
}
else
{
direction = 'l';
--rightLimit;
--xPos;
}
break;
}
}
// Now, just dump the matrix contents to stdout
for (int i = 0; i < number; ++i)
{
for (int j = 0; j < number; ++j)
{
Console.Write("{0}\t", matrix[i][j]);
}
Console.Write("\n");
}
Console.ReadLine();
}
It crashes and gives the error:
Index must be within the bounds of the List.
Parameter name: index
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是否需要该列表列表,或者您可以使用二维数组(您通过数字传递维度,为什么需要列表?)
Is that list of lists required, or can you use an array with 2 dimensions (you are passing the dimension through number, why do you need the lists?)
如果您尝试在大于列表当前计数的索引处插入(),则会引发该异常。最有可能的是,您正在以这样的方式遍历“矩阵”,即语句
插入到大小尚未达到 yPos 的列表中。避免这种情况的最简单方法是首先向每个内部列表添加足够的元素:
That exception is thrown if you try to Insert() at an index that is larger than the current Count of the list. Most likely you are traversing the "matrix" in such a way that the statement
is inserting into a list that is not yet of size yPos. The easiest way to avoid this is to add enough elements to each inner list initially:
如果没有在我的脑海中运行代码,这一行
看起来很可疑,也许你应该从 0 开始,但话又说回来,我可能完全关闭了。
Without running the code in my mind, this line
looks suspicious, may be you should start with 0, but then again, I might be totally off.