【发布时间】:2009-04-03 23:59:22
【问题描述】:
为什么下面的代码会抛出异常?
for (int i = 0; i <= Items.Length-1; i++)
{
Console.WriteLine(Items[i,1]);
}
例外:
System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="Es"
StackTrace:
at Es.Program.Main(String[] args) in C:\Users\Fero\Documents\Visual Studio 2005\Projects\Es\Es\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
物品声明:
获取字符串数组的函数:
static string[,] ReadFromFile(string filename, int rowsF)
{
StreamReader SR;
string S;
string[] S_split;
SR = File.OpenText(filename);
S = SR.ReadLine();
string[,] myItems = new String[rowsF, 2];
int row_number = 0;
while (S != null)
{
S_split = S.Split('"');
//temp_items[row_number,0] =
myItems[row_number,0] = S_split[1];
myItems[row_number,1] = S_split[2];
row_number++;
S = SR.ReadLine();
}
SR.Close();
return myItems;
}
string[,] Items = ReadFromFile(myFile, rowsF);
【问题讨论】:
-
数组声明是什么样子的?
-
可以包含Items的声明吗?
-
我已经添加了一些东西来发布
-
第 119 行是 (Console.WriteLine) ?
-
如果够短,我们能看到输入文件吗?