• 步步为营-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
  • 相关阅读:
    Java HashMap 和 ConcurrentHashMap
    递归算法应用
    二叉树基础知识
    自动删除qq空间说说
    移动APP测试的22条军规--笔记
    SQL Server数据库状态监控
    SqlSugar-事务操作
    详解第一范式、第二范式、第三范式、BCNF范式
    SQL 日期
    2019年世界各国gdp排名对比
  • 原文地址:https://www.cnblogs.com/YK2012/p/6715656.html
Copyright © 2020-2023  润新知