【发布时间】:2017-01-24 20:26:18
【问题描述】:
下面这段代码生成了一个未处理的 IndexOutOfRangeException,但我不知道为什么。 a for-loop 设置为小于i for-loop,因为数组的数组是 3x2。我确实尝试过捕食 i 和 a 但没有运气。你能看到错误吗?
namespace text_test
{
class txt_program
{
// () You don't use "args" in the method
public void txt()
{
int[][] names = { new int[] { 2, 3, 4}, new int[] { 5, 6, 7} };
using (StreamWriter SW = new StreamWriter(@"txt.txt"))
{
for (int i = 0; i < 4; i++)
{
for (int a = 0; a < 3; a++)
{
SW.Write(" " + names[i][a]);
}
SW.WriteLine(names);
}
}
}
}
}
预期的输出将是一个 .txt 文件:
2 3 4
5 6 7
【问题讨论】:
-
for (int i = 0; i < 4; i++)-i从 0 到 3,数组从 0 到 2。但是这里有一个提示:如果你有这样的问题,在调试器下运行它,看看它在哪里中断.
标签: c# arrays multidimensional-array streamwriter