class MergeArray
    {
        public int[] MergeTwoArray(int[] testArray1, int[] testArray2)
        {
            int array1Length = testArray1.Length;
            int array2Length = testArray2.Length;
            int[] testArray3 = new int[1];            int i = 0;
            int j = 0;
            while (i < array1Length && j < array2Length)
            {
                if (testArray1[i] < testArray2[j])
                {
                    testArray3[testArray3.Length - 1] = testArray1[i];
                    i++;
                    Array.Resize(ref testArray3, testArray3.Length + 1);
                }
                else if (testArray1[i] > testArray2[j])
                {
                    testArray3[testArray3.Length - 1] = testArray2[j];
                    j++;
                    Array.Resize(ref testArray3, testArray3.Length + 1);
                }
                else
                {
                    testArray3[testArray3.Length - 1] = testArray1[i];
                    i++;
                    Array.Resize(ref testArray3, testArray3.Length + 1);
                }
            }
            if (i == array1Length)
            {
                while (j < array2Length)
                {
                    testArray3[testArray3.Length - 1] = testArray2[j];
                    j++;
                    Array.Resize(ref testArray3, testArray3.Length + 1);
                }
            }
            if (j == array2Length)
            {
                while (i < array1Length)
                {
                    testArray3[testArray3.Length - 1] = testArray1[i];
                    i++;
                    Array.Resize(ref testArray3, testArray3.Length + 1);
                }
            }            return testArray3;
        }
    }    class Test
    {
        static void Main(string[] args)
        {
            MergeArray ma = new MergeArray();
            int[] test1 = { 2, 3, 6, 7, 9, 21};
            int[] test2 = {3, 8, 9, 12, 15, 20, 26, 56 };
            int[] test3 = ma.MergeTwoArray(test1, test2);
            string s = string.Empty;
            foreach(int i in test3)
            {
                //Console.WriteLine(i);
                s = s + i.ToString() + " ";
            }
            Console.WriteLine(s);
        }
    }实际的运行结果:2 3 3 6 7 8 9 9 12 15 20 21 26 56 0预期的结果: 2 3 3 6 7 8 9 9 12 15 20 21 26 56 期待各位高手帮我解答下。
BTW:
不需要使用arraylist,list<int>,只需要看下我当前的代码,哪里逻辑chu了问题,谢谢!

解决方案 »

  1.   


                int[] a = { 2, 3, 6, 7, 9, 21 };
                int[] b = { 3, 8, 9, 12, 15, 20, 26, 56 };
                int[] c = a.Concat(b).ToArray();
                Array.Sort(c);            string s = string.Empty;
                foreach (int i in c)
                {
                    s = s + i.ToString() + " ";
                }
                Console.WriteLine(s);象这样就可以了。
      

  2.   

    至于你的代码为什么最后多一个0
    那是因为你每次向testArray3添加元素后,都把数组Resize大了一号。
    Array.Resize(ref testArray3, testArray3.Length + 1);
    这样一来最后一次添加元素后,testArray3的长度就会比实际长度大一号。
    最后一次不需要Resize
      

  3.   

    haukwong 谢谢你啊,我修改了代码,在合并最后一个元素的时候加了判断,问题解决了