【发布时间】:2013-03-23 04:45:20
【问题描述】:
我似乎无法弄清楚如何使用锯齿状数组和文件。我有三个带有数字的文件,并且想将每个文件读入它自己的数组中。这就是我到目前为止所拥有的。试图填充 [0] 数组但无济于事。任何帮助表示赞赏。也找不到任何有关执行此操作的教程。
private void button1_Click(object sender, EventArgs e)
{
StreamWriter section1;
StreamWriter section2;
StreamWriter section3;
StreamReader section1read;
StreamReader section2read;
StreamReader section3read;
section1 = File.CreateText("Section1.txt");
section2 = File.CreateText("Section2.txt");
section3 = File.CreateText("Section3.txt");
int[][] Scores = new int[3][];
Random randnum = new Random();
for (int i = 0; i < 12; ++i)
{
int num = randnum.Next(55, 99);
section1.WriteLine(num);
}
for (int j = 0; j < 8; ++j)
{
int num1 = randnum.Next(55, 99);
section2.WriteLine(num1);
}
for (int k = 0; k < 10; ++k)
{
int num3 = randnum.Next(55, 99);
section3.WriteLine(num3);
}
section1.Close();
section2.Close();
section3.Close();
section1read = File.OpenText("Section1.txt");
int nums = 0;
while (!section1read.EndOfStream)
{
Scores[0][nums] = int.Parse(section1read.ReadLine());
++nums;
}
for (int i = 0; i < Scores.Length; ++i)
{
listBox1.Items.Add(Scores[0][i]);
}
section1read.Close();
}
【问题讨论】:
-
Scores.Length-->Scores[0].Length?出了什么问题?编译错误,异常? -
列表框中有什么显示吗?看起来 Scores[0].Length 的错误只会导致第一个项目出现。
-
我在 section1read.ReadLine() 上收到 NullReferenceException 未处理错误
-
@AaronLS 没有出现在列表中
-
Scores是如何声明和初始化的?您需要显示所有代码。
标签: c# file jagged-arrays