专注于.Net

享受编程的乐趣
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

两数组相比筛选数据

Posted on 2008-12-05 16:37  古道飘零客  阅读(287)  评论(0编辑  收藏  举报
static void Main(string[] args)
        {
           
int[] a = new int[] { 2, 4, 5, 7, 8 };
           
int[] b = new int[] { 9, 4, 5, 6, 7, 8 };
           
//不要用数组 老出错
              ArrayList alist = new ArrayList(a);
              ArrayList blist
= new ArrayList(b);
            ArrayList same
= new ArrayList();
            ArrayList aDif
= new ArrayList();
            ArrayList bDif
= new ArrayList();

          
foreach(object o in alist)
           {
              
if (blist.Contains(o))
               {
                   same.Add(o);
               }
              
else {
                   aDif.Add(o);
               }
           }
           
           
foreach (object o in blist)
           {
              
if (!same.Contains(o))
               {
                   bDif.Add(o);
               }
           }


            Console.WriteLine(
"相同的");
           
foreach (Object o in same)
                Console.Write(
"   {0}", o);
            Console.WriteLine();
            Console.WriteLine(
"a中特有的");
           
foreach (Object o in aDif)
                Console.Write(
"   {0}", o);
            Console.WriteLine();
            Console.WriteLine(
"b中特有的");
           
foreach (Object o in bDif)
                Console.Write(
"   {0}", o);

            Console.Read();
        }