9月11日作业 (泛型)和统计字母个数

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

namespace _03Dictionary
{
    class Program
    {
        static void Main(string[] args)
        {
            #region Myregion
            // Dictionary<string, string> dict = new Dictionary<string, string>();
           // dict.Add("yqq","杨巧巧");
           // dict.Add("lxf", "刘晓飞");
           // dict.Add("zl", "张兰");
           // if(!dict.ContainsKey("ly"))
           // {
           // dict.Add("ly","刘一");
           // }
           //Console.WriteLine( dict.ContainsKey("zl"));
           //Console.WriteLine(dict["zl"]);

           //foreach (string item in dict.Values)
           //{
           //    Console.WriteLine(item);
            //}
            #endregion

            #region MyRegion2

            Dictionary<string, Person> dict = new Dictionary<string, Person>();
            Person p1 = new Person() { Name = "张兰", Age = 18 };

            dict.Add(p1.Name, p1);
            Console.WriteLine((dict[p1.Name]).Name+"的年龄是:"+dict[p1.Name].Age);

            //1遍历键
            foreach (string item in dict.Keys)
            {
                Console.WriteLine(item);
            }
            //2遍历值
            foreach (Person item in dict.Values)
            {
                Console.WriteLine(item);
            }
            //3键和值同时遍历
            foreach (KeyValuePair<string, Person> kv in dict)
            {
                Console.WriteLine("键:{0},值:{1}",kv.Key,kv.Value);
            }
            #endregion
            //4.计算字符串中每种字符出现的次数。(面试题)“welcome to china! this

//is a beautiful county, i think you will like it.here is The great wall

//”提示:Dictionary<char,int>

            string str = "Welcome to china! this is a beautiful county, i think you will like it.here is The great wall";
            str = str.ToLower();
            Dictionary<char, int> dict2 = new Dictionary<char, int>();//
            //dict<'w',3>
            //统计字符出现的次数
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsLetter(str[i]))
                {
                    if (dict2.ContainsKey(str[i]))
                    {
                        dict2[str[i]]++;
                    }
                    else
                    {
                        dict2.Add(str[i], 1);
                    }
                }
            }

            foreach (KeyValuePair<char, int> kv in dict2)
            {

                Console.WriteLine("字符:{0},出现了{1}次",kv.Key,kv.Value);
            }

                Console.ReadKey();


        }
    }
    public class Person
    {
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值