步步为营-11-List<T>泛型的简单练习

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

namespace 集合简单练习
{
    class Program
    {
        static void Main(string[] args)
        {
            
        }

        private static void Test3()
        {
            //奇偶分拣,奇数在前偶数在后
            List<int> result = new List<int>() { 12, 564, 32, 87, 15, 35, 54, 798, 153, 54 };

            List<int> oddList = new List<int>();
            List<int> evenList = new List<int>();
            foreach (var item in result)
            {
                if (item % 2 == 0)
                {
                    evenList.Add(item);
                }
                else
                {
                    oddList.Add(item);
                }
            }
            oddList.AddRange(evenList);
            ShowList(oddList);
        }

        private static void Test2()
        {
            //随机生成10个1---100之间的数放入到list中.1:不能重复.2:都是偶数
            int count = 0;
            List<int> result = new List<int>();
            while (count < 10)
            {
                Random rom = new Random();
                int item = rom.Next(1, 101);
                if (!result.Contains(item) && item % 2 == 0)
                {
                    result.Add(item);
                    count++;
                }
            }
            ShowList(result);
            Console.ReadLine();
        }

        private static void Test1()
        {
            //把两个集合去掉重复后合并成一个集合号
            List<string> lista = new List<string> { "a", "b", "c", "d" };
            List<string> listb = new List<string> { "c", "d", "e", "f", "g" };
            foreach (string item in listb)
            {
                if (!lista.Contains(item))
                {
                    lista.Add(item);
                }
            }
            ShowList(lista);
            Console.ReadLine();
        }
        //展示集合
        private static void ShowList(List<int> lista)
        {
            foreach (var item in lista)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
        private static void ShowList(List<string> lista)
        {
            foreach (var item in lista)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}
ListTest

 Dictionary的练习

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

namespace DictoryTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //统计单词出现次数
            string word = "hello,world.my name is DictoryTest";
            Dictionary<char, int> dt = new Dictionary<char, int>();
            foreach (var item in word)
            {
                if (!dt.Keys.Contains(item))
                {
                    dt.Add(item, 1);
                }
                else 
                {
                    dt[item] = dt[item]+1;
                }
            }
            foreach (KeyValuePair<char, int> kv in dt)
            {
                Console.WriteLine(kv.Key +"一共出现了"+kv.Value+"");
            }

            Console.Read();
        }
    }
}
View Code

 

posted @ 2017-04-15 21:09  逍遥小天狼  阅读(140)  评论(0编辑  收藏  举报