编译错误:索引超出范围
我想编写合并排序算法,当我调试程序并向其提供数字时,它会出现索引超出范围错误,我的代码有什么问题?提前致谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,
IndexOutOfRangeException
不是一个编译错误,它是一个运行时错误。数组中的索引是从 0 开始的。例如,这意味着长度为 3 的数组具有索引 0、1 和 2,但索引 3 不存在且超出范围。要修复错误,请将以下行中的
<=
更改为<
:等等...
您的程序中也可能存在其他错误 - 这只是第一个错误我看到了。
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:etc...
There may be other errors in your program too - this was just the first one I saw.
C# 中的数组是从零开始的,这意味着数组中的第一项位于索引 0,而不是索引 1。
但是,
Length
属性返回从 1 开始的计数数组中对象的数量。因此,当您编写x <= nums.Length
时,您实际上是在尝试访问超出数组边界的索引。相反,您应该将代码的该部分重写为:
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 writex <= 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:
索引从
0
开始,所以你应该这样做:Index starts from
0
so you should do: