泛型集合List<>

来源:互联网 发布:美工培训机构 编辑:程序博客网 时间:2024/05/17 05:53

      泛型是具有占位符(类型参数)的类、结构、接口和方法,这些占位符是类、结构、接口和方法所存储或使用的一个或多个类型的占位符。
      泛型集合类可以将类型参数用作它所存储的对象的类型的占位符;类型参数作为其字段的类型和其方法的参数类型出现。泛型方法可以将其类型参数用作其返回值的类型或者其形参的
类型之一。

      List 是对应于 ArrayList 的泛型类。 Dictionary 是对应于 Hashtable 的泛型类。

     “泛型类型定义”是用作模板的类、结构或接口声明,其中具有该类、结构或接口声明可以包含或使用的类型的占位符。

例如,ary 类可以包含两种类型:键和值。因为它只是一个模板,您不能创建作为泛型类型定义的类、结构或接口的实例。
 

      “泛型类型参数”或称“类型参数”是泛型类型或方法定义中的占位符。Dictionary 泛型类型具有两个类型参数:TKey 和 TValue,分别表示其键和值的类型。


      “构造泛型类型”或称“构造类型”是为泛型类型定义的泛型类型参数指定类型得到的结果。

      “泛型类型参数”是替换泛型类型参数的任何类型。 


       一般术语“泛型类型”包括构造类型和泛型类型定义。

 

       “约束”是加在泛型类型参数上的限制。例如,可以将类型参数限制为实现 IComparer 泛型接口的类型以确保可以对该类型的实例进行排序。还可以将类型参数限制为具有特定基类的类型、具有默认构造函数的类型或是引用类型或值类型。泛型类型的用户不能替换不满足这些约束的类型参数。

      “泛型方法定义”是具有两个参数列表的方法:一个泛型类型参数列表和一个形参列表。
许多泛型集合类型是非泛型类型的直接模拟。Dictionary 是 Hashtable 的泛型版本;它使用泛型结构 KeyValuePair 而不是 DictionaryEntry 进行枚举。

 
                //1.分拣奇数偶数的程序用泛型实现。(List<int>)
                /*string str = "2 4 7 9 6 8 5";
                string[] nums = str.Split(' ');
                List<int> listOdd = new List<int>();
                List<int> listEven = new List<int>();
                for (int i = 0; i < nums.Length; i++)
                {
                    if (Convert.ToInt32(nums[i]) % 2 != 0)
                    {
                        listOdd.Add(Convert.ToInt32(nums[i]));
                    }
                    else
                    {
                        listEven.Add(Convert.ToInt32(nums[i]));
                    }
                }
                listOdd.AddRange(listEven);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < listOdd.Count; i++)
                {
                    sb.Append(listOdd[i] + " ");
                }
                Console.WriteLine(sb);
                */


            //2.将int数组中的奇数放到一个新的int数组中返回。
            /*
            int[] numbers = { 2, 7, 9, 4, 3, 8, 1, 6 };
            List<int> listOdd = new List<int>();
            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] % 2 != 0)
                {
                    listOdd.Add(numbers[i]);
                }
            }
            int[] numOdd = listOdd.ToArray();
            for (int i = 0; i < numOdd.Length; i++)
            {
                Console.WriteLine(numOdd[i]);
            }
             */

             //3.从整数的List<int>中找最大值,不适用Max方法。
            
  
            //int[] numbers = { 2, 7, 9, 4, 3, 8, 1, 6 };
            /*
            Random random = new Random();     
            List<int> list = new List<int>();    
            for (int i = 0; i <10; i++)
            {
                int ra = random.Next(1, 101);
                list.Add(ra);
                Console.WriteLine(list[i]);
            }
            int temp = list[0];
            for (int n = 1; n < list.Count; n++)
            {
                if (temp < list[n])
                {
                    temp = list[n];
                }
            }
            Console.WriteLine("max is:" + temp);
             * /
        

                //4.计算字符串中每种字符出现的次数
                /*string str = "welcome to china! this is a beautiful county, i think ou will like it.here is The great wall";
                str = str.ToLower();
                Dictionary<char, int> dict1 = new Dictionary<char, int>();
                //统计字符出现的次数
                for (int i = 0; i < str.Length; i++)
                {
                    if (char.IsLetter(str[i]))
                    {
                        if (dict1.ContainsKey(str[i]))
                        {
                            dict1[str[i]]++;
                        }
                        else
                        {
                            dict1.Add(str[i], 1);
                        }
                    }
                }
                foreach (KeyValuePair<char, int> kv in dict1)
                {
                    Console.WriteLine("字符:{0},出现了{1}次",kv.Key,kv.Value);
                }*/