【问题标题】:Index was outside the bounds of the array while reading from a file从文件中读取时,索引超出了数组的范围
【发布时间】:2018-10-19 06:41:21
【问题描述】:

我希望你们都度过了愉快的一天。所以我修复了我的程序的一个错误,但还有另一个错误:/

所以这是我创建并从文件中读取数据的代码:

  void ReadData(string fileName, Branch[] branches)
    {
        string shopsName = null;
        using (StreamReader reader = new StreamReader(@fileName))
        {
            string line = null;
            line = reader.ReadLine();
            if (line != null)
            {
                shopsName = line;
            }
            Branch tempBranches = TempBranch(branches, shopsName);
            string address = reader.ReadLine();
            string phoneNumber = reader.ReadLine();
            while (null != (line = reader.ReadLine()))
            {
                string[] values = line.Split(';');
                string facturer = values[0];
                string model = values[1];
                double capacity = double.Parse(values[2]);
                string energyClass = values[3];
                string assemblyType = values[4];
                string color = values[5];
                string attribute = values[6];
                double cost = double.Parse(values[7]);
                Fridges fridge = new Fridges(facturer, model, capacity, energyClass, assemblyType, color, attribute, cost);
                tempBranches.fridges.AddFridge(fridge);
            }
        }

还有我使用 TempBranch 方法的代码。错误在这一行:if (branches[i].ShopsName == shopsName)。希望您能帮助我,因为我昨天尝试修复此问题 30 分钟,但仍然无法正常工作:D

private static Branch TempBranch(Branch[] branches, string shopsName)
    {
        for (int i = 0; i < MaxNumberOfFridges; i++)
        {
            if (branches[i].ShopsName == shopsName)
            {
                return branches[i];
            }
        }
        return null;
    }

【问题讨论】:

  • 在 C# 中,如果变量实现了接口 IEnumerable,您可以执行 foreach 循环。试试这个:foreach(Branch b in branches) {if (b.ShopsName == shopsName) return b;} 然后在循环之外你可以返回 null 就像你做的那样没有
  • 问题在这里int i = 0; i &lt; MaxNumberOfFridges; i++,所以改为这个int i = 0; i &lt; branches.Length; i++

标签: c#


【解决方案1】:

如果您将MaxNumberOfFridges 替换为branches.Length,它只会尝试查找位于branches 数组范围内的Branch。它不起作用的原因是因为您尝试访问的索引大于数组的Length。

【讨论】:

    【解决方案2】:

    试试这个。如果您不知道数组的长度,请使用 foreach。

    private static Branch TempBranch(Branch[] branches, string shopsName)
        {
            foreach(var branch in branches)
            {
                if (branch.ShopsName == shopsName)
                {
                    return branch;
                }
            }
            return null;
        }
    

    【讨论】:

    • 如果你不知道数组的长度,那branches.Length呢?
    • 是的,你是对的。 Tbh foreach 是更简洁的解决方案,可防止 = 等拼写错误。而且语法更简单。
    【解决方案3】:

    您也可以尝试使用 LINQ 查询,

    return branches.Where(b => b.ShopsName == shopsName).FirstOrDefault(); 
    

    编辑:

    在您的新帖子中发生的 NullReferenceError 是由于在您创建商店的函数中返回 null 而发生的。这是因为没有找到给定的店铺名称。

    因此它尝试将冰箱添加到不存在的商店,这是不可能的。您必须添加一个检查,以免发生这种情况。

    【讨论】:

      【解决方案4】:

      此错误是因为MaxNumberOfFridges 大于分支length.. 为简化它,假设MaxNumberOfFridges 为20 但arry length 为10,因此您正在尝试访问数组中的元素11这超出了数组长度。

      修复它

        for (int i = 0; i < branches.Length; i++)
          {
              if (branches[i].ShopsName == shopsName)
              {
                  return branches[i];
              }
          }
      

      其他选项是使用 foreach 循环

      foreach(var b in branches)
      {
          if (b.ShopsName == shopsName)
          {
              return branches[i];
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-23
        • 2020-03-28
        • 2012-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多