简体   繁体   English

不明白为什么我会收到 IndexOutOfRangeException

[英]Don't understand why I'm getting an IndexOutOfRangeException

I am trying to load in data field names into my program from a text file, and store them into a variable, an array.我试图将数据字段名称从文本文件加载到我的程序中,并将它们存储到一个变量中,一个数组。

The field names are to be used to sort and print data from other files into an organized new document.字段名称用于将其他文件中的数据排序并打印到有组织的新文档中。

I'm having a lot of trouble specifically with storing the fields from the file into an array.我在将文件中的字段存储到数组中时遇到了很多麻烦。

The data fields text file is organized in a list manner, with each field being on its own row.数据字段文本文件以列表方式组织,每个字段都在自己的行上。

I went with creating a while loop that runs until the Peek() of the StreamReader is equal to -1.我创建了一个 while 循环,该循环一直运行到 StreamReader 的 Peek() 等于 -1。

I nested a for loop within the while loop that increments the index integer variable by 1 until it's less than or equal to the total amount of rows in the text file.我在 while 循环中嵌套了一个 for 循环,它将索引整数变量递增 1,直到它小于或等于文本文件中的总行数。

It also uses ReadLine() to store a row of the text document into the array at the specific index.它还使用 ReadLine() 将文本文档的一行存储到特定索引处的数组中。 I thought using that in the for loop would cycle through each row and store what it needs to store.我认为在 for 循环中使用它会循环遍历每一行并存储它需要存储的内容。

I will need to use the fields within a dictionary so that I can use the fields as keys and the data from other documents as the values for the dictionary when I get around to displaying that information.我将需要使用字典中的字段,以便在我开始显示该信息时可以将这些字段用作键,并将来自其他文档的数据用作字典的值。

I thought the way I had done it would have the necessary measures to avoid the IndexOutOfRangeException, but I guess that is not the case.我认为我这样做的方式将有必要的措施来避免 IndexOutOfRangeException,但我想情况并非如此。

I would appreciate anyone willing to help me with this.我会很感激任何愿意帮助我的人。 I apologize if anything is unclear and will try to clarify things if need be.如果有任何不清楚的地方,我深表歉意,并会在需要时尝试澄清。

If my attempted logic explanation was terrible, here's the code:如果我尝试的逻辑解释很糟糕,这里是代码:

    class Program
{
    protected static string[] dataFields = new string[] { };

    static void Main(string[] args)
    {
        int iIndex;

        using (StreamReader titleStream = new StreamReader(@"..\..\DataFieldsLayout.txt"))
        {
            while (titleStream.Peek() > -1)
            {
                for (iIndex = 0; iIndex <= 150; iIndex++)
                {
                    // where the exception occurs
                    dataFields[iIndex] = titleStream.ReadLine();
                }
            }
        }

        // test
        Console.WriteLine(dataFields[0]);
        Console.WriteLine(dataFields[1]);
        Console.WriteLine(dataFields[2]);
        Console.WriteLine(dataFields[3]);
        Console.WriteLine(dataFields[4]);
    }
}

This is because when you define an array you have to make room for each element.这是因为当你定义一个数组时,你必须为每个元素腾出空间。 So, when you declare the array as you have done you are not giving it any space to accept new data.因此,当您像所做的那样声明数组时,您并没有给它任何空间来接受新数据。 You might want to declare your array in the following way (so that it has enough space for all of the data):您可能希望以下列方式声明您的数组(以便它有足够的空间容纳所有数据):

protected static string[] dataFields = new string[151];

You are reading up to 151 elements (because your <= 150 condition), so you want to give the appropriate amount of space.您正在阅读多达 151 个元素(因为您的<= 150条件),因此您需要提供适当的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM