【问题标题】:Identifying an index out of bounds in C#在 C# 中识别超出范围的索引
【发布时间】:2013-03-30 03:52:05
【问题描述】:

所以我有这段代码可以从 txt 中对虚构的人口普查数据进行排序。文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Program
{
    const int SIZE = 900;
    const int SIZEDISTRICT = 22;
    const int RANGE = 5;
    static void Main()
    {   
        //These arrays will hold the split data from a text file.
        int[] districtDataD = new int[900];
        string[] districtDataG = new string[900];
        string[] districtDataM = new string[900];
        int[] districtDataA = new int[900];

        //countDistrict will hold how many hypothetical people in each hypothetical district and 
        //ages will hold how many hypothetical people between certain hypothetical ages.
        int[] countDistrict = new int[SIZEDISTRICT];
        int[] ages = new int[RANGE] { 0, 18, 30, 45, 65};


        //Modules
        ReadFile(districtDataD, districtDataG, districtDataM,districtDataA);
        CountPopulation(districtDataD, countDistrict);
        AgeRanges(districtDataA, ages);
        DisplayData(districtDataD, districtDataA, ages);
    }//End Main


    //This module splits and inserts the data into the four first arrays
    static void ReadFile(int[] districtDataD, string[] districtDataG, string[] districtDataM, int[] districtDataA)
    {
        string[] lines = File.ReadAllLines("census.txt");
        int i = 0;

        while (i < SIZE && i < districtDataD.Length)
        {
            foreach (string line in File.ReadAllLines("census.txt"))
            {
                string[] parts = line.Split(',');

                districtDataD[i] = int.Parse(parts[0]);
                districtDataG[i] = parts[1];
                districtDataM[i] = parts[2];
                districtDataA[i] = int.Parse(parts[3]);
                i++;
            }
        }
    }
    //This module counts how many hypothetical people are in each fictional district
   static void CountPopulation(int[] districtDataD, int[] countDistrict)
    {
        int i = 0;
        for (i = 0; i < districtDataD.Length; i++)
        {
            if (districtDataD[i] > 0 && districtDataD[i] < districtDataD.Length)
            {
                countDistrict[districtDataD[i]]
                    ++;
            }
        }
    }


    //This module sorts the ages into 0-18, 19-30, 31-45, 46-65, and 65 and up
     private static void AgeRanges(int[] districtDataA, int[] ages)
     {
         int idx = 0;
         for (idx = 0; idx < districtDataA.Length && ages[idx] > districtDataA[idx]; idx++)
         {

             ages[idx] = districtDataA[idx];
         }
     }


    //This module displays the data
     static void DisplayData(int[] countDistrict, int[] ageDistrict, int[] ages)
    {
        int index = 0;
        for (index = 0; index < countDistrict.Length; index++)
        {
            Console.WriteLine(" District {0}: {1}", index + 1, countDistrict[index]);
        }

        int x = 0;
        for (x = 0; x < ageDistrict.Length; x++)
        {
            Console.WriteLine("Ages under {0} : {1}", ages[x], ageDistrict[x]);
        }
    }
}

我得到了索引越界错误,但我不知道在哪里以及如何找到它。

文本。文件目前看起来像这样,但将扩展为包含更多,大约 100 个左右。 900 只是用作上限。

21,f,s,14

41,f,m,22

12、m、s、12

11、f、s、8

29,米,米,4

6、m、s、12

9, f, s, 2

30, f, s, 1

【问题讨论】:

  • 如果你不知道需要存储多少个元素,那就不要使用数组;请改用 List 之类的集合。
  • 你应该得到一个堆栈跟踪,它告诉你什么方法和什么代码行号给出了错误。此外,尝试调试并在抛出异常时停止。
  • 如何为每个循环添加一个 SIZE 变量?

标签: c# arrays runtime-error indexing


【解决方案1】:

如果您不知道数组的长度,请改用集合。这将使您的文件大小与长度无关。使用数组,您必须设置数组的长度,据我了解,该长度可以根据文件进行更改 - 如果没有正确实施,这总是会给您带来 index out of bound 的困难。使用集合可以避免遇到这些问题

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

然后您可以将项目添加到此集合中,例如:

districtDataD.Add(1);

【讨论】:

  • @Sabotenderizer 你是什么意思?
【解决方案2】:

您很可能在以下位置遇到错误:

            string[] parts = line.Split(',');
            districtDataD[i] = int.Parse(parts[0]);
            districtDataG[i] = parts[1];
            districtDataM[i] = parts[2];
            districtDataA[i] = int.Parse(parts[3]);

使用前检查parts.Length。可能是您的任何一行都不包含 3 个逗号(',')。

另一个可能的错误来源是:

countDistrict[districtDataD[i]]

【讨论】:

  • 目前没有一个逗号少于或多于 3 个。
  • 我认为 countDistrict[districtDataD[i]] ++;是不完整的陈述。如果是,你能完成吗???
  • 我之前用另一个程序尝试过,它似乎可以工作?
  • 检查了 Visual Studio 上的调试内容。我将第一个 while 循环更改为 (i
  • 你似乎在这个网站上很活跃,我还能怎么联系你,你忙吗,因为我还在为此苦苦挣扎。我的意思是整个事情。
猜你喜欢
  • 2012-12-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多