我想編寫合併排序算法,當我調試程序並給它的數字,它goves索引超出範圍錯誤,我的代碼是什麼問題?提前感謝。編譯錯誤:索引超出範圍
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;
}
}
解決了,謝謝你,但你說有其他的問題,也因爲它給出了輸出0 – Arash 2010-12-18 08:00:25