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

本文通过几个实例展示了如何使用C#中的List和Dictionary来解决实际问题,包括奇偶数分拣、生成不重复偶数及统计字符出现频率等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/YK2012/p/6715656.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值