hashtable 泛型 C#

iamzxf發表於2015-05-09


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hashtableT
{
    class Program
    {
        static void randomNumber(int []arr, int n)
        {
            for (int i = 0; i < n; i++)
                arr[i] = new Random().Next(101);
        }

        static void PrintElement<T>(HashSet<T> hs)
        {
            foreach (T e in hs)
                Console.Write("{0,5}",e);
            Console.WriteLine();
        }

        static bool isLessThan(int e)
        {
            return e < 5;
        }

        static void Main(string[] args)
        {
            HashSet<int> hs1 = new HashSet<int>();
            HashSet<int> hs2 = new HashSet<int>();

            for (int i = 1; i <= 10; i++)
                hs1.Add(i);

            for (int i = 2; i <= 15; i += 2)
                hs2.Add(i);

            Console.WriteLine("hs1中的元素");
            PrintElement<int>(hs1);

            Console.WriteLine("hs2中的元素");
            PrintElement<int>(hs2);

            Console.WriteLine("hs1中是否包含3:"+hs1.Contains(3));
            Console.WriteLine("hs1是否等於hs2:"+hs1.Equals(hs2));
            Console.WriteLine("hs1和hs2有重疊:"+hs1.Overlaps(hs2));

            hs1.UnionWith(hs2);
            Console.WriteLine("二者的並集:");
            PrintElement<int>(hs1);

            Console.WriteLine("hs2中的元素是hs1的子集:"+hs2.IsSubsetOf(hs1));
            hs1.ExceptWith(hs2);

            Console.WriteLine("二者的差集:");
            PrintElement<int>(hs1);

            hs1.Add(2);
            hs1.IntersectWith(hs2);
            Console.WriteLine("hs1中增加元素2以後與hs2的交集:");
            PrintElement<int>(hs1);

            hs2.RemoveWhere(isLessThan);
            Console.WriteLine("hs2中刪除小於5的元素後:");
            PrintElement<int>(hs2);

            Console.ReadLine();
        }
    }
}



相關文章