C# 容器综合练习
第一题:字典基础练习
1、创建一个字典dictA,其中Key为字符串,Value为整数。
2、为这个字典添加"a"到"z"共26个Key,值为随机1~10的数字。
3、对字典dictA,删除所有值为偶数的键值对。
4、再次创建同样的字典dictB,也同样删除值为偶数的键。
5、然后将这两个字典合并成一个新字典。Key相同的部分,以dictA的值为准
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp9
{
class Program
{
static void InitDic(Dictionary<char, int> dic)
{
Random r = new Random();
string a = $"a";
int asc = Asc(a);
for (int i = 0; i < 26; i++)
{
dic.Add((char)asc, r.Next(1, 10));
asc++;
}
}
public static int Asc(string s)
{
if(s.Length == 1)
{
ASCIIEncoding a = new ASCIIEncoding();
int b = (int)a.GetBytes(s)[0];
return b;
}
else
{
throw new Exception("String is not vaild");
}
}
public static char Cha(string s)
{
int a = int.Parse(s);
if(a >=0 && a <= 255)
{
char c = (char)a;
return c;
}
else
{
throw new Exception("String is not vaild");
}
}
static void PrintDic(Dictionary<char,int> dict)
{
foreach(var pair in dict)
{
Console.WriteLine(pair.Key + " " + pair.Value);
}
}
static void Deleven(Dictionary<char,int> dict)
{
List<char> L = new List<char>();
foreach(var pair in dict)
{
if (pair.Value % 2 == 0)
{
L.Add(pair.Key);
}
}
foreach(var key in L)
{
dict.Remove(key);
}
}
static void CombineAB(Dictionary<char, int> DA, Dictionary<char, int> DB, Dictionary<char, int> DC)
{
foreach (var pair in DA)
{
DC[pair.Key] = pair.Value;
}
foreach (var pair in DB)
{
if (!DA.ContainsKey(pair.Key))
{
DC[pair.Key] = pair.Value;
}
}
}
static void CombineAB2(Dictionary<char, int> DA, Dictionary<char, int> DB, Dictionary<char, int> DC)
{
foreach (var pair in DB)
{
DC[pair.Key] = pair.Value;
}
foreach (var pair in DA)
{
DC[pair.Key] = pair.Value;
}
}
static void Main(string[] args)
{
Dictionary<char, int> dicA = new Dictionary<char, int>();
InitDic(dicA);
PrintDic(dicA);
Console.WriteLine();
Deleven(dicA);
PrintDic(dicA);
Console.WriteLine();
Dictionary<char, int> dicB = new Dictionary<char, int>();
InitDic(dicB);
PrintDic(dicB);
Console.WriteLine();
Deleven(dicB);
PrintDic(dicB);
Console.WriteLine();
Dictionary<char, int> dicC = new Dictionary<char, int>();
CombineAB(dicA,dicB,dicC);
//或者 CombineAB2(dicA,dicB,dicC);
PrintDic(dicC);
}
}
}
第二题:字典和列表转换
1、随机生成一个string列表
2、将这个列表的第一项作为key,第二项作为value;第三项作为key,第四项作为value……依次类推。生成一个新的字典
3、再将这个新的字典转换成一个新的列表
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp10
{
class Program
{
static string Randomxiang()
{
Random R = new Random();
string str = string.Empty;
string Rch = "ABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789abcdefghijklmnopqrstuvwxyz";
//string[] Rch = new string[R.Next(0, 10000)];
for (int i = 0; i < 2; i++)
{
str += Rch[R.Next(Rch.Length)];
}
return str;
}
static string[] Randomstr(int length)
{
string[] strs = new string[length];
for (int i = 0; i < length; i++)
{
strs[i] = Randomxiang();
}
return strs;
}
static void PrintString(string[] str)
{
for (int i = 0; i < str.Length; i++)
{
Console.Write(str[i]);
Console.Write(" ");
}
Console.WriteLine();
}
static void PrintDic(Dictionary<string, string> dict)
{
foreach (var pair in dict)
{
Console.WriteLine("(" + pair.Key + " " + pair.Value + ")");
}
}
static List<string> CrList(Dictionary<string,string> dict)
{
List<string> L = new List<string>();
foreach(var pair in dict)
{
L.Add(pair.Key);
L.Add(pair.Value);
}
return L;
}
static void PrintList(List<string> list)
{
for(int i = 0; i < list.Count; i++)
{
Console.Write(list[i] + " ");
}
}
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
string[] str = Randomstr(n);
PrintString(str);
Dictionary<string, string> D1 = new Dictionary<string, string>();
for (int i = 0; i < n; i += 2)
{
D1.Add(str[i], str[i + 1]);
}
PrintDic(D1);
PrintList(CrList(D1));
}
}
}
第三题:C#进阶 字典嵌套
1、创建一个字典dictA,其中Key为字符串,Value为整数。
2、为这个字典添加"a"到"z"共26个Key,值为随机1~10的数字。
3、再次用同样的方式创建字典dictB。
建立一个复杂的新字典Dictionary<string, List<int>> dictC;
将dictA和dictB合并,Key相同的部分,要保留所有的值
(A和B可能存在重复的Key,但是由于dictC的值是list,所以可以保留多个值)。
(101条消息) ⭐️C# 零基础到进阶⭐️| 字典和列表 相互嵌套使用 的终极总结!_呆呆敲代码的小Y的博客-CSDN博客_c#中对嵌套型list进行增删
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void InitDic(Dictionary<char,int> dict)
{
Random r = new Random();
string a = $"a";
int asc = Asc(a);
for(int i = 0; i < 26; i++)
{
dict.Add((char)asc, r.Next(1, 10));
asc++;
}
}
public static int Asc(string s)
{
if (s.Length == 1)
{
ASCIIEncoding a = new ASCIIEncoding();
int b = (int)a.GetBytes(s)[0];
return b;
}
else
{
throw new Exception("String is not vaild");
}
}
public static char Cha(string s)
{
int a = int.Parse(s);
if(a>0 && a < 255)
{
char c = (char)a;
return c;
}
else
{
throw new Exception("String is not vaild");
}
}
static void PrintDic(Dictionary<char,int> dict)
{
foreach(var Pair in dict)
{
Console.Write("("+Pair.Key + " " + Pair.Value+")");
}
Console.WriteLine();
}
static void PrintDicC(Dictionary<string,List<int>> dict)
{
foreach (var pair in dict)
{
foreach(var val in pair.Value)
{
Console.Write("(" + pair.Key + " " + val + ")");
}
}
Console.WriteLine();
}
static void Main()
{
Dictionary<char, int> dictA = new Dictionary<char, int>();
InitDic(dictA);
PrintDic(dictA);
Dictionary<char, int> dictB = new Dictionary<char, int>();
InitDic(dictB);
PrintDic(dictB);
Dictionary<string, List<int>> dictC = new Dictionary<string, List<int>>();
foreach(var paira in dictA)
{
List<int> list = new List<int>();
string str = paira.Key.ToString();
foreach(var pairb in dictB)
{
if(pairb.Key == paira.Key)
{
list.Add(paira.Value);
list.Add(pairb.Value);
}
}
dictC.Add(str,list);
}
PrintDicC(dictC);
}
}
}