以下代码生成了一个未处理的IndexOutOfRangeException,但我不知道为什么。 a for-loop
设置为小于i for-loop
,因为数组数组是3x2。我确实试图捕食我和一个但没有运气。你能看到错误吗?
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
答案 0 :(得分:0)
循环A重复4次而不是2次
使用GetLength()
for (int i = 0; i < names.GetLength(0); i++)
{
for (int a = 0; a < names.GetLength(1); a++)
{
SW.Write(" " + names[i][a]);
}
SW.WriteLine(names);
}