【问题标题】:Out of bounds error on iterating over array遍历数组时出现越界错误
【发布时间】:2021-09-30 13:23:17
【问题描述】:

所以这个程序让我很头疼,我们被要求在列表更合适的地方使用数组,所以这不是我的选择。

经过几个小时的工作,我差不多完成了。然后就会发生这种情况。


Console.Clear();

Console.WriteLine("Would you like to search for an entry? Y/N."); // check if user wants to search.

Answer = Convert.ToString(Console.ReadLine());
Answer = Answer.ToUpper();
Console.Clear();

if (Answer == "Y")
{
    Console.WriteLine("Please enter the game you'd like to search for: "); // ask user for value
    search = Convert.ToString(Console.Read());

    for (i = 0; i <= 20; i++) // run through array of names, checking if the entered value is present
 

        if (search.ToLower() == Name[i].ToLower() && i <20)
        {
            Console.WriteLine("Match Found!"); // Inform user there's a matching entry.
            Console.WriteLine(" ");
            Console.WriteLine("Game: " + Name[i] + ". Publisher: " + Publisher[i] + ". Genre: " + Genre[i]);

        }
        else
            Console.WriteLine("No match found! Check spelling."); // Inform user of no match.
}              
{
    Console.WriteLine("Clearing lines"); //start cleanup.
    Console.Clear();
    Console.WriteLine("End of program. Thank you for logging data. Press any key to exit");
    Console.ReadKey();
}

这是一段代码,引用的数组的上限为20。

【问题讨论】:

    标签: c# .net visual-studio console-application


    【解决方案1】:

    由于数组中有 20 个值,因此您的 for 循环条件应为 i &lt; 20

    在您的情况下,它会检查 21 个值,因为您有 20 个,所以它会引发异常,因为没有 Name[20]

    你的代码应该是这样的:

    if (Answer == "Y")
    {
        Console.WriteLine("Please enter the game you'd like to search for: ");
        search = Convert.ToString(Console.Read());
    
        for (i = 0; i < 20; i++)
            if (search.ToLower() == Name[i].ToLower())
            {
                Console.WriteLine("Match Found!"); 
                Console.WriteLine(" ");
                Console.WriteLine("Game: " + Name[i] + ". Publisher: " + Publisher[i] + ". Genre: " + Genre[i]);
    
            }
            else
                Console.WriteLine("No match found! Check spelling."); 
    }          
    

    编辑:

    如果您不确定数组中有多少元素和/或数组在运行时调整大小,您可以将i &lt; 20 替换为i &lt; Name.Length

    【讨论】:

      【解决方案2】:

      好吧,因为我们没有看到原始数组,所以我们不能确定它真的有 20 个项目。而且我们不能确定在一段时间内,数组不会被调整大小(也许你会添加一些项目)。你想要的是在for 循环的每次检查中检查数组大小,如下所示:

      for (i = 0; i < Name.Length; i++)
      

      另请注意,我已将符号从 &lt;= 更改为 &lt;,这导致了您的越界异常。那是因为您正在迭代 20 个项目的数组,从 0 开始索引。

      array index -->  index started from 1
            0     -->       1.
            1     -->       2.
            2     -->       3.
                   .
                   .
           19     -->      20.  //this is the last item
           20     -->      21.  //out-of-bounds, will fail
      

      【讨论】:

        【解决方案3】:

        您正在迭代数组 0-20。我假设您的数组大小为 20,因此它应该从 0-19 迭代。

         for (i = 0; i < 20; i++)
        

        或者

         for (i = 0; i <= 19; i++)
        

        【讨论】:

          【解决方案4】:

          数组具有从零开始的索引,因此您需要更改 for 循环:

          如果您的数组声明为 string[] Name = new string[20];,则索引范围为 0-19

          for (i = 0; i < 20; i++)
          

          for(i = 0; i < Name.Length; i++)
          

          元素是数组中的一个值。数组的长度是 它可以包含的元素总数。数组的下限 是它的第一个元素的索引。数组可以有任何下限, 但默认情况下它的下限为零。不同的下限 可以在创建 Array 类的实例时使用 创建实例。多维数组可以有不同的界限 每个维度。一个数组最多可以有 32 维。

          system.array

          【讨论】:

          • Name.Length 给了我同样的问题,但这解决了它。
          • @Raskunda .Length 将为您提供可能元素的数量,因此您必须迭代到比 .Length 少 1,因为它以从零开始的索引开始。它起作用的原因是因为我将for 循环更改为比.Length 或声明的值20 少一。
          猜你喜欢
          • 1970-01-01
          • 2022-11-10
          • 1970-01-01
          • 1970-01-01
          • 2016-02-17
          • 2015-08-10
          • 1970-01-01
          • 2013-04-02
          • 1970-01-01
          相关资源
          最近更新 更多