简体   繁体   English

C# 索引超出范围

[英]C# Index out of bounds

I am trying to make a linear search where the user inputs a selection of numbers and then they input a random number and the program shows if it is in the list or not.我正在尝试进行线性搜索,用户输入一组数字,然后他们输入一个随机数,程序会显示它是否在列表中。

    int[] list = new int[10];
        bool found = false;
        for (int i = 0; i < 12;)
        {
            Console.WriteLine("Enter number to be stored.");
            list[i] =Convert.ToInt16(Console.ReadLine());
            i++;
        }
        Console.WriteLine("Enter number you want to find.");
        int n1 = Convert.ToInt16(Console.ReadLine());
        for (int i = 0; i <= 10;)
        {
            if (list[i] == n1)
            {
                Console.WriteLine(n1 + " is in the list.");
                found = true;
                Console.ReadKey();
            }
            else i++;

        }
        if (found == false)
        {
            Console.WriteLine("Element not in this list.");
        }
        Console.ReadKey();

I am pretty sure that the problem lies in this snippet of code.我很确定问题出在这段代码中。

    int[] list = new int[10];
    bool found = false;
    for (int i = 0; i < 12;)
    {
        Console.WriteLine("Enter number to be stored.");
        list[i] =Convert.ToInt16(Console.ReadLine());
        i++;
    }

An array beings with 0 so there are 11 element spaces right?一个 0 的数组,所以有 11 个元素空间,对吗? So when i run it, i go past entering the 10th number and when i enter the 11th, it breaks and says所以当我运行它时,我 go 过去输入第 10 个数字,当我输入第 11 个数字时,它会中断并说

    System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

Firstly I'd recommend you read up on for loops and arrays in C#.首先,我建议您阅读 C# 中的for循环和 arrays。

Secondly you don't want to hardcode the length in the for loop - let the framework do it for you using list.Length .其次,您不想在 for 循环中硬编码长度 - 让框架使用list.Length为您完成。 You're seeing the crash because you're trying to assign a value to your array, but the index within your array doesn't exist.您看到崩溃是因为您尝试为数组分配值,但数组中的索引不存在。

int[] list = new int[10];
bool found = false;
for (int i = 0; i < list.Length; i++)
{
   // Do work with list[i]
}

When you say int[10];当你说int[10]; you tell it to have 10 entries.你告诉它有10个条目。 They are counting from zero, yes, but that just means that your array has the indices 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 .它们从零开始计数,是的,但这仅意味着您的数组具有索引0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Your loop goes from i = 0 up to i = 11 before it stops because it's no longer smaller than twelve.您的循环在停止之前从i = 0i = 11 ,因为它不再小于十二。 But the index for the array can at most be 9 .但是数组的索引最多可以是9

If you set a breakpoint, you should be able to look at the array contents in a debugger.如果设置断点,您应该能够在调试器中查看数组内容。 This way, you could also test such things yourself.这样,您也可以自己测试这些东西。

It should be i < 10 or i <= 9 if your array only holds 10 objects.如果您的数组仅包含 10 个对象,则它应该是 i < 10 或 i <= 9。 The 10 indices will be 0-9. 10 个指数为 0-9。

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

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