我要实现的是对两个字符串数组取并集,并且要对最后的结果进行排序操作(如:abcde...);现在,我实现了并集的操作,但最后的排序操作我没能实现,希望大牛们帮下忙,下面是我的代码:using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;namespace ConsoleApplication3
{
    class Program
    {
        /// <summary>
        /// 下面的程序主要是对两个字符串取并集;
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string str1 = "a,b,c,h,p";
            string str2 = "adefghjk";
            ///将字符串转化为数组类型
            ///
            string[] a = str1.ToString().Split(',');
            ///与str2相比较。
            ///
            for (int i = 0; i <a.Length;i++ )
            {
                if (str2.Contains(a[i]) == true)
                { ///说明有相同的元素在该字符串里面,进行下次操作;
                    ///
                    continue;                }
                else { 
                
                    str2+=a[i];
                
                }
            }           
            Console.WriteLine("最后的结果如下:\n"+str2);
            Console.ReadKey();
                 }
    }
}

解决方案 »

  1.   

    var query = from aOne in a            
               select one;
    foreach (var s in query)
    {
        Console.WriteLine(s);
    }
      

  2.   

    加上 order by 忘记了
      

  3.   

        String[] arr1 = {"abc", "df", "abc"}; 
            String[] arr2 = {"abc", "cc", "df", "d", "abc"}; 
            String[] result_union = union(arr1, arr2); 
            System.out.println("求并集的结果如下:"); 
            for (String str : result_union) { 
                System.out.println(str); 
            } 
      

  4.   

    原始排序法string[] source = { "d", "y", "n", "", "y", "y", "j", "b", "c", "a"};foreach (string s in BubbleSort(source))
    {
        Response.Write(" " + s); //a b c d j n y y y 
    }//冒泡排序
    public static string[] BubbleSort(string[] input)
    {
        string[] result = (string[])input.Clone(); //保存排序后的结果    for (int i = 0; i < input.Length - 1; i++)  //length - 1次
        {
            for (int j = i + 1; j < input.Length; j++)
            {
                if (string.Compare(result[i], result[j]) > 0)
                {
                    string temp = result[i];
                    result[i] = result[j];
                    result[j] = temp;
                }
            }
        }    return result;
    }
      

  5.   

    不好意思,忘了看你是在WinForm里了
    把Response.Write换成你的Console.WriteLine
      

  6.   

            string str1 = "a,b,c,h,p";
            string str2 = "adefghjk";
            List<string> list = new List<string>();
            foreach (char c in str2)
                list.Add(c.ToString());
            IEnumerable<string> istr = str1.Split(',').Union(list).OrderBy(x => x);
            foreach (string i in istr)
                Response.Write(i);
      

  7.   


    using System;
    using System.Collections.Generic;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String str1 = "a,b,c,h,p";
                String str2 = "adefghjk";            List<Char> list = new List<Char>();            foreach (Char c1 in str1)
                    foreach (Char c2 in str2)
                        if (c1 != ',' && c1 == c2)
                            list.Add(c1);            list.Sort();            foreach (Char c in list)
                    Console.WriteLine(c);            //输出:
                //a
                //h
            }
        }
    }
      

  8.   

    将你的代码最后面改了下
    using System;
    using System.Collections.Generic;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str1 = "a,b,c,h,p";
                string str2 = "adefghjk";
                ///将字符串转化为数组类型
                ///
                string[] a = str1.ToString().Split(',');
                ///与str2相比较。
                ///
                for (int i = 0; i < a.Length; i++)
                {
                    if (str2.Contains(a[i]) == true)
                    { ///说明有相同的元素在该字符串里面,进行下次操作;
                        ///
                        continue;
                    }
                    else
                    {
                        str2 += a[i];
                    }
                }            List<Char> list = new List<Char>();
                list.AddRange(str2.ToCharArray());
                list.Sort();            Console.WriteLine("最后的结果如下:");
                foreach (Char c in list)
                    Console.WriteLine(c);
            }
        }
    }
      

  9.   

    一、辞不达意不可怕,怕的是南辕北辙
    “我要实现的是对两个字符串数组取并集,并且要对最后的结果进行排序操作(如:abcde...)”
    可是你的代码,写的却是取2个字符串中字符的并集!二、工欲达其事,必先利其器
    援引MSDN:建议面向 2.0 版的所有应用程序都使用新的泛型集合类,而不要使用旧的非泛型集合类,如 ArrayList。有关更多信息,请参见 .NET Framework 类库中的泛型(C# 编程指南)。
    // 省略将2个串分割为char[]的过程。Split()或者ToCharArray()
    char[] left = new char[] {'h','a','b','d','i'};
    char[] right = new char[] { 'j', 'c', 'e', 'a', 'f' };
    SortedSet<char> chars = new SortedSet<char>();AddWithArrayOfChar(left, chars);
    AddWithArrayOfChar(right, chars);foreach (char c in chars)
    {
    Console.WriteLine(c);
    }
    private static void AddWithArrayOfChar(char[] array, SortedSet<char> set)
    {
    foreach (char c in array)
    {
    if (set.Contains(c))
    continue;
    else
    set.Add(c);
    }
    }
    结果:
    a
    b
    c
    d
    e
    f
    h
    i
    j
    请按任意键继续. . .