Pergunta

I can't seem to figure out how to work with Jagged Arrays and Files. I have three files with numbers in them and wan to read each file into its own array. This is what I have so far. Was trying to populate the [0] array but to no avail. Any help appreciated. Can't find any tutorials on doing this, either.

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();
}
Foi útil?

Solução

Jagged arrays should be initialized in two steps:

  1. The array itself:

    int[][] Scores = new int[3][];
    
  2. The sub-arrays:

    Scores[0] = new int[12];
    Scores[1] = new int[8];
    Scores[2] = new int[10];
    

Array is a fixed length data structure. If you don't know the size in advance, you have to use a dynamic length structure. The best option is List<> class:

List<List<int>> scores = new List<List<int>>();

scores.Add( new List<int>() );

using( StreamReader section1read = File.OpenText("Section1.txt"))
{
    string line;
    while ((line = section1read.ReadLine()) != null)
    {
        scores[0].Add(int.Parse(line));
    }
}

Here are other things to consider:

  • Use a using block make sure any unmanaged resources associated with file is disposes.
  • You can check the return value of StreamReader.ReadLine() to determine the end of file

Outras dicas

http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Quote:

"Before you can use jaggedArray, its elements must be initialized. You can initialize the elements like this:

jaggedArray[0] = new int[5];

jaggedArray[1] = new int[4];

jaggedArray[2] = new int[2];"

So, what you're doing in your code is initializing a jagged array of three int[]s that are all set to null. If you don't create the array at each index before attempting to assign to it, nothing is there.

It seems what you want, though, is dynamic allocation - you don't know how many integers you need to store when you wrote the program. In this case, you should learn about the class List<>. List<> is like an array except you can add to and remove from the number of elements it has at runtime, rather than declaring it has a fixed size, using Add and Remove. http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top