【发布时间】:2015-08-12 09:44:06
【问题描述】:
我创建了一个类Matrix。我创建了一种填充矩阵的方法,但出现错误。有什么问题?
class Matrix
{
private static int n, m;
private string[,] arr = new string[n, m];
public int N
{
set
{
n = value;
}
get
{
return n;
}
}
public int M
{
set
{
m = value;
}
get
{
return m;
}
}
public string[,] SetMatrix()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
arr[i, j] = Console.ReadLine();
}
}
return arr;
}
}
static void Main(string[] args)
{
Matrix matrix = new Matrix();
Console.WriteLine("- enter n");
matrix.N = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("- enter m");
matrix.M = Convert.ToInt32(Console.ReadLine());
string[,] arr= new string[matrix.N, matrix.M];
Console.WriteLine("enter matrix data");
arr = matrix.SetMatrix(); //Error at this line
Console.ReadKey();
}
错误:
An unhandled exception of type "System.IndexOutOfRangeException" in Matrix.exe
欲了解更多信息:Index was outside the bounds of the array.
提前致谢。
【问题讨论】:
-
什么错误? ehat 是
spreadSheet变量吗? -
所以在方法
SetTable中抛出了错误,但是您向我们展示了方法SetMatrix。愿意展示真正的方法吗?还有什么错误? -
SpreadsheetSimulator.exe 中“System.IndexOutOfRangeException”类型的未处理异常有关详细信息:索引超出了数组的范围。
-
@ВладимирНагорный,这应该是问题的一部分,而不是评论。你如何初始化你在
SetMatrix中引用的arr? -
尝试用
i++和j++替换++i和++j