现在有A,B两个String数组
A的值为{“a1”,“a5”}
B的值为{"a1","a2","a3","a4","a5","a6"}
   有什么方法可以去掉B数组中 与A数组中相同的元素?
  或者 合并两个数组去掉相同元素也行

解决方案 »

  1.   

    用for循环来做,在其内写if/else来判断。
      

  2.   

    先用split把两个数组中的值分离出来.然后foreach循环,去掉B中等于A的值.
      

  3.   


    void Main()
    {
     string[] a={"a1","a5"};
    string[] b={"a1","a2","a3","a4","a5","a6"};
    b = b.Except(a).ToArray();
    foreach (string s in b)
    {
    Console.WriteLine(s);
    }}
    //结果:
    a2
    a3
    a4
    a6
      

  4.   

    string[] arr1= new string[3]{"a","b","c"};
    string[] arr2= new string[7] { "a","b","d","e","f","g","h"};
    string[] temparr1= arr1.Except(arr2).ToArray();
    string[] temparr2 = arr2.Except(arr1).ToArray();
      

  5.   

    int[] a = { 1, 2, 3, 4, 5, 9 };
    int[] b = { 1, 4, 5, 7, 8, 9 };
    List<int> la = new List<int>(a);
    List<int> lb = new List<int>(b);
    List<int> lc = new List<int>();
    foreach (int i in la)
    {
      if (!lb.Contains(i))
      {
      lc.Add(i);
      }
    }
    foreach (int i in lb)
    {
      if (!la.Contains(i))
      {
      lc.Add(i);
      }
    }
    LINQ Intersect(交集),Except(差集).   
    int[] result= A.Except(B).ToArray();
      

  6.   

    試試吧,應該沒有錯!
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] A = new string[] { "a1", "a5" };
                string[] B = new string[] { "a1", "a2", "a3", "a4", "a5", "a6" };
                List<string> all = new List<string>();
                for (int i = 0; i < A.Length; i++)
                {
                    if (!all.Contains(A[i]))
                        all.Add(A[i]);
                }
                for (int j = 0; j < B.Length; j++)
                {
                    if (!all.Contains(B[j]))
                        all.Add(B[j]);
                }
                all.Sort();
                for (int i = 0; i < all.Count; i++)
                {
                    Console.WriteLine(all[i]);
                }            Console.ReadLine();
            }
        }
    }
      

  7.   

    int i,j,k;
    for(i=0;i<6;i++) {for(j=0;j<2;j++)   if(b[i]!=a[j])
        {
          c[k]=b[i];
          k++;
        }
     }
    return c[k];
    看到楼主的题目,我就随手写下来了,本想用C#写写看的,可是觉得是C。呵呵,路过留下脚印。
      

  8.   

    最简单的方法:String[] a = {"a1","a5" };
                String[] b = {"a1","a2","a3","a4","a5","a6","a1" };            b= b.Except(a).ToArray<String>();