编译错误:索引超出范围

发布于 2024-10-08 05:15:33 字数 1606 浏览 0 评论 0原文

我想编写合并排序算法,当我调试程序并向其提供数字时,它会出现索引超出范围错误,我的代码有什么问题?提前致谢。

    private void button3_Click(object sender, EventArgs e)
    {


        string[] source = textBox1.Text.Split(',');
        string[] source1 = textBox3.Text.Split(',');
        int[] nums2 = new int[source1.Length + source.Length];
        int[] nums = new int[source.Length];
        for (int i = 0; i < source.Length; i++)
        {
            nums[i] = Convert.ToInt32(source[i]);

        }
        int[] nums1 = new int[source1.Length];
        for (int j = 0; j < source1.Length; j++)
        {
            nums1[j] = Convert.ToInt32(source1[j]);
        }
        int x=0;
        int y=0;
           int z=0;

        while (x <=nums.Length && y <= nums1.Length)
        {
            if (nums[x] <= nums1[y])///it gives out of range on this line
            {
                nums2[z] = nums[x];
                x++;

            }
            else
            {
                nums2[z] = nums1[y];
                y++;
            }

            z++;
        }
        if (x > nums.Length)
        {
            while (y <= nums1.Length)
            {
                nums2[z] = nums1[y];

                z++;
                y++;
            }
            if (y > nums1.Length)
            {
                while (x <= nums.Length)
                {
                    nums2[z] = nums[x];
                    z++;
                    x++;
                }
            }
        }
        string merge = nums2[z].ToString();

       textBox4.Text = merge;

    }
}

i want to write merge sort algorithm,when i debug the program and give numbers to it,it goves index out of range error,whats the problem of my code?thanks in advance.

    private void button3_Click(object sender, EventArgs e)
    {


        string[] source = textBox1.Text.Split(',');
        string[] source1 = textBox3.Text.Split(',');
        int[] nums2 = new int[source1.Length + source.Length];
        int[] nums = new int[source.Length];
        for (int i = 0; i < source.Length; i++)
        {
            nums[i] = Convert.ToInt32(source[i]);

        }
        int[] nums1 = new int[source1.Length];
        for (int j = 0; j < source1.Length; j++)
        {
            nums1[j] = Convert.ToInt32(source1[j]);
        }
        int x=0;
        int y=0;
           int z=0;

        while (x <=nums.Length && y <= nums1.Length)
        {
            if (nums[x] <= nums1[y])///it gives out of range on this line
            {
                nums2[z] = nums[x];
                x++;

            }
            else
            {
                nums2[z] = nums1[y];
                y++;
            }

            z++;
        }
        if (x > nums.Length)
        {
            while (y <= nums1.Length)
            {
                nums2[z] = nums1[y];

                z++;
                y++;
            }
            if (y > nums1.Length)
            {
                while (x <= nums.Length)
                {
                    nums2[z] = nums[x];
                    z++;
                    x++;
                }
            }
        }
        string merge = nums2[z].ToString();

       textBox4.Text = merge;

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瓜 2024-10-15 05:15:33

首先, IndexOutOfRangeException 不是一个编译错误,它是一个运行时错误。

数组中的索引是从 0 开始的。例如,这意味着长度为 3 的数组具有索引 0、1 和 2,但索引 3 不存在且超出范围。要修复错误,请将以下行中的 <= 更改为 <

while (x < nums.Length && y < nums1.Length)

while (y < nums1.Length)

while (x < nums.Length)

等等...

您的程序中也可能存在其他错误 - 这只是第一个错误我看到了。

First, an IndexOutOfRangeException is not a compile error, it's a runtime error.

The indexes in an array are 0-based. This means for example that an array of length 3 has indexes 0, 1 and 2, but index 3 doesn't exist and is out of range. To fix your error change <= to < on the following lines:

while (x < nums.Length && y < nums1.Length)

while (y < nums1.Length)

while (x < nums.Length)

etc...

There may be other errors in your program too - this was just the first one I saw.

糖粟与秋泊 2024-10-15 05:15:33

C# 中的数组是从零开始的,这意味着数组中的第一项位于索引 0,而不是索引 1。

但是,Length 属性返回从 1 开始的计数数组中对象的数量。因此,当您编写 x <= nums.Length 时,您实际上是在尝试访问超出数组边界的索引。

相反,您应该将代码的该部分重写为:

    while (x < nums.Length && y <  nums1.Length)
    {
        if (nums[x] <= nums1[y])
        {
            nums2[z] = nums[x];
            x++;

        }

    // etc.

Arrays are zero-based in C#, meaning that the first item in the array is at index 0, not index 1.

However, the Length property returns a one-based count of the number of objects in the array. So when you write x <= nums.Length, you're actually trying to access an index that it outside of the bounds of the array.

Instead, you should rewrite that section of your code as:

    while (x < nums.Length && y <  nums1.Length)
    {
        if (nums[x] <= nums1[y])
        {
            nums2[z] = nums[x];
            x++;

        }

    // etc.
请止步禁区 2024-10-15 05:15:33

索引从 0 开始,所以你应该这样做:

while (x < nums.Length && y < nums1.Length)

Index starts from 0 so you should do:

while (x < nums.Length && y < nums1.Length)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文