数组一:1,2.3,4,5,6,7,8,9
数组二、1,5,8
有什么好办法取出它们不一样的?
就是:2,3,4,6,7,9现在已经晕菜了!
大家帮忙

解决方案 »

  1.   

    用2.0的新功能吧。数据list应该有,findall功能。比较实用。不要用循环老方法了。呵
      

  2.   


                int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                int[] arr2 = { 1, 5, 8 };
                List<int> list = new List<int>();
                foreach (int i in arr1)
                    if (Array.IndexOf(arr2, i) < 0)
                        list.Add(i);
                int[] result = list.ToArray();
      

  3.   

    linq版:            int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                int[] arr2 = { 1, 5, 8 };
                var arr = from i in arr1
                          where Array.IndexOf(arr2, i) < 0
                          select i;
                int[] result = arr.ToArray();
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //楼主的两个数组
                int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                int[] array2 = { 1, 5, 8 };            //遍历array1,取出每一个元素去跟array2比较,若有相同的元素存在则设置exist为真
                foreach (int i in array1)
                {
                    bool exist = false;
                    foreach(int j in array2)
                    {
                        if (i == j)
                            exist = true;
                    }
                //若exist为假,则输出元素
                    if (!exist)
                        Console.Write(i+" ");
                }
                //暂停
                Console.ReadKey();
            }
        }
    }//以上代码在Microsoft Visual C# 2008 速成版 SP1 - 简体中文 中通过
      

  5.   

    如果用vs2008的话,
    int[] arr1 ={ 1,2,3,4,5,6,7,8,9 };
    int[] arr2 = { 1, 5, 8 };
    var result = arr1.Where(a=>!arr2.Contains(a));
    foreach (var n in result)
    {
       Console.Write(n+" ");
    }
      

  6.   

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //楼主的两个数组
                int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                int[] array2 = { 1, 5, 8 };            //遍历array1,用array2.Contains(i)来判断是否包含i
                foreach (int i in array1)
                {
                    if (!array2.Contains(i))
                        Console.Write(i+" ");
                }
                //暂停
                Console.ReadKey();
            }
        }
    }//以上代码在Microsoft Visual C# 2008 速成版 SP1 - 简体中文 中通过更简洁明了,受ojlovecd前辈的启发。PS:请问如何自动高亮代码?
      

  7.   


    List<int> group1 = new List<int>();
                group1.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
                List<int> group2 = new List<int>(new int[] { 1,5,8});
                List<int> group3 = new List<int>(); // The number that belong to group1 ,but not belong group2
                List<int> group4 = new List<int>(); // The number that belong to group2 ,but not belong group1
                foreach (int nu1 in group1)
                {
                    if (!group2.Contains(nu1))
                    {
                        group3.Add(nu1);
                    }
                }
                foreach (int nu2 in group2)
                {
                    if (!group2.Contains(nu2))
                    {
                        group4.Add(nu2);
                    }
                }            List<int> groupALL = new List<int>();
                groupALL.AddRange(group3);
                groupALL.AddRange(group4);
                MessageBox.Show(group3.Count.ToString());
                MessageBox.Show(group4.Count.ToString());
      

  8.   

    在提供一种方法:            int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                int[] arr2 = { 1, 5, 8 };
                List<int> list = new List<int>(arr1);
                foreach (int i in arr2)
                    list.Remove(i);
                int[] result = list.ToArray();
      

  9.   

    vs2008
               
                int[] arrint1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                int[] arrint2 = { 1,5,8,45};
               
                int[] retint1 = arrint1.Concat(arrint2).Distinct().ToArray();//合并两数组
               
                int[] retint2 = arrint1.Intersect(arrint2).ToArray(); //取得两数组相同的
                
                int[] retint = retint1.Except(retint2).ToArray(); //去除相同的           
      

  10.   

    net3.0 扩展版String res=String.Empty;
            int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] arr2 = { 1, 5, 8 };
            Array.ForEach<int>(arr1, delegate(int a) {
                if(!arr2.Contains(a))
                {
                    res+=a.ToString();
                }
            });还有linq版的,实际大同小异,就不写了
      

  11.   

            public static int[] FindDifferent(int[] a1, int[] a2)
            {
                    List<int> list = new List<int>();
                    list.AddRange(a1);
                    for (int i=0;i<a2.Length ;i++)
                    {
                        if (list.Contains (a2[i]))
                        list.Remove (a2[i]);
                    }
                    int[] result = list.ToArray();
                    return  result ;
            }
      

  12.   

      晕 ,刚才只算了一个数组里面的,惭愧啊.
            public static int[] FindDifferent(int[] a1, int[] a2) 
            { 
                    List <int> list = new List <int>(); 
                    List <int> list1 = new List <int>(); 
                    list.AddRange(a1); 
                    for (int i=0;i <a2.Length ;i++) 
                    { 
                        if (list.Contains(a2[i]))
                        {
                            list.Remove(a2[i]);
                        }
                        else
                        {
                            list1.Add(a2[i]);
                        }
                    } 
                    list.AddRange(list1);
                    int[] result = list.ToArray(); 
                    return  result ; 
            }