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;
}
}
}