集合类List与Dictonary实例练习

时间:2022-09-18 09:08:01

a、List<>泛型集合 

复制代码代码如下:


View Code 
using System; 
using System.Collections.Generic; 
namespace _02_泛型集合 { 
class Person { 
public Person(string name, int age) { 
this .name = name; 
this .age = age; 

private string name; 
public string Name { 
get { 
return name; 

set { 
name = value ; 


private int age; 
public int Age { 
get { 
return age; 

set { 
age = value ; 



class Student : Person { 
public Student(string name, int age) 
: base (name, age) { 


class Teacher : Person { 
public Teacher(string name, int age) 
: base (name, age) { 


class Program { 
static void Main( string[] args) { 
Student xyy = new Student( "小月月" , 12); 
Student fj = new Student( "凤姐" , 14); 
Student fr = new Student( "芙蓉姐姐" , 16); 
Student xl = new Student( "犀利哥" , 18); 
List <Student > student = new List <Student >(); 
student.Add(xyy); 
student.Add(fj); 
student.Add(fr); 
student.Add(xl); 
Teacher tea = new Teacher( "wanghao" , 21); 
//student.Add(tea);//因为List<Student> 限制类型必须为Student 所以不能添加Teacher对象 
//比ArrayList更加限制存储的类型 而且效率要高 因为添加 取出对象是不会发生装箱 拆箱的操作的 
//遍历 
foreach (Student item in student) { 
Console .WriteLine(item.Name);//因为返回的就是student对象 所以可以直接读取属性值 
Console .WriteLine(item.Age); 

//移除 
student.Remove(xyy); //移除的是引用地址 所以下面移除的s不是xyy 
Student s = new Student( "小月月" , 12); 
student.Remove(s); 
student.RemoveAt(0); 
student.RemoveRange(0, 2); //从0索引移除后面两位 即前移除前两位学生 xyy fj 
//student.RemoveAll();//移除所有满足条件的 详见帮助文档 
student.Clear(); 
Console .WriteLine(student.Contains(xyy)); 
//ToArray()方法 转换学生类型数组 
Student [] stu = student.ToArray(); 
foreach (Student item in stu) { 
Console .WriteLine(item.Name); 

Console .Read(); 



b、Dictonary<>字典 

复制代码代码如下:


View Code 
using System; 
using System.Collections.Generic; 
namespace _03_泛型集合 { 
class Student { 
public Student(string name, int age) { 
this .name = name; 
this .age = age; 

private string name; 
public string Name { 
get { 
return name; 

set { 
name = value ; 


private int age; 
public int Age { 
get { 
return age; 

set { 
age = value ; 



class Program { 
static void Main( string[] args) { 
Student xyy = new Student( "小月月" , 12); 
Student fj = new Student( "凤姐" , 14); 
Student fr = new Student( "芙蓉姐姐" , 16); 
Student xl = new Student( "犀利哥" , 18); 
Dictionary <string , Student> student = new Dictionary < string, Student >();//key为string类型的name value为Student对象 
student.Add( "小月月" , xyy); 
student.Add( "凤姐" , fj); 
student.Add( "芙蓉姐姐" , fr); 
student.Add( "犀利哥" , xl); 
Console .WriteLine(student["犀利哥" ].Name); //根据key获取value 
//遍历 通过key 
foreach (string item in student.Keys) { 
Console .WriteLine(item); 
Console .WriteLine(student[item].Age); 

//遍历 通过value 
foreach (Student item in student.Values) { 
Console .WriteLine(item.Age); 

//遍历键值对 
foreach (KeyValuePair < string, Student > item in student) { 
Console .WriteLine(item.Key); 
Console .WriteLine(item.Value.Age);//item.Value是Student对象 直接使用 

//移除 
//student.Remove("小月月"); 
//student.Clear(); 
student.ContainsKey( "小月月" ); //是否包含该key 
//更多参见帮助文档 
Console .Read(); 



c、泛型集合练习 

复制代码代码如下:


View Code 
using System; 
using System.Collections.Generic; 
namespace _04__泛型练习 { 
class Program { 
static void Main( string[] args) { 
//把分拣奇偶数的程序用泛型实现 
string str = "7 4 3 2 9 8 33 22" ; 
string [] strs = str.Split(' ' ); 
strs = Getevent(strs).ToArray(); 
string res = string .Join( " ", strs); //string数组 直接用join就好了 
Console .WriteLine(res); 
//将int数组中的奇数放到一个新的int数组中返回 
int [] intarr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
List <int > list = new List <int >(); 
foreach (int item in intarr) { 
if (item % 2 != 0) { 
list.Add(item); 


intarr = list.ToArray(); 
foreach (int item in intarr) { 
Console .WriteLine(item); 

//从一个整数的List<int>中取出最大数。不使用自身带的Max()方法。 
List <int > list2 = new List <int > { 1, 2, 3, 4, 5, 6, 7, 8 }; 
int max = list2[0]; 
foreach (int item in list2) { 
if (item > max) { 
max = item; 


Console .WriteLine("泛型集合最大值为{0}" , max); 
Console .ReadKey(); 

public static List< string > Getevent(string [] str) { 
List <string > list = new List <string >(); 
List <string > list2 = new List <string >(); 
foreach (string item in str) { 
if (int .Parse(item) % 2 != 0) { 
list.Add(item); 
} else { 
list2.Add(item); 


list.AddRange(list2); 
return list; 



d、泛型集合练习2 

复制代码代码如下:


View Code 
using System; 
using System.Collections.Generic; 
namespace _06_泛型集合练习 { 
class Program { 
static void Main( string[] args) { 
//把1,2,3转换为壹贰叁 
string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零" ; 
Dictionary <char , char> money = new Dictionary < char, char >(); 
string [] strs = str.Split(' ' ); 
string s = "123456789" ; 
string news = "" ; 
for (int i = 0; i < strs.Length; i++) { 
money.Add(strs[i][0], strs[i][1]); 

foreach (char item in s) { 
news += money[item]; 

Console .WriteLine(news); 
char n = '1' ; 
Console .WriteLine(n + ' ' ); //结果显示为81 char字符相加是把其Asic码相加的 
Console .WriteLine(n + 2);//结果显示为51 
//如果想实现字符串相加就把任一个参数改变为tostring输出 
Console .WriteLine(n.ToString() + 2);//显示为12 
//计算字符串中每种字符出现的次数(面试题)。“Welcome to Chinaworld”,不区分大小写,打印“W 2” “e 2” “o 3”…… 
string str2 = "Welcome to Chinaworld" ; 
Dictionary <char , int> num = new Dictionary < char, int >(); 
foreach (char item in str2.ToLower().Replace( " " , "" )) { 
if (num.ContainsKey(item)) { 
num[item]++; 
} else { 
num.Add(item, 1); 


foreach (var key in num.Keys) { 
Console .WriteLine("\"{0}{1}\"" , key, num[key]); 

//编写一个函数进行日期转换,将输入的中文日期转换为阿拉伯数字日期,比如:二零一二年十二月月二十一日要转换为2012-12-21。 
string date = "二零一二年十二月二十一日" ; //date2的转换 需要考虑十左右字符是否都在字典中 在则为 则十消失 如果左边不在右边在则变1 如果左边在右边不在则变0 如果左右都不在则变10 还是蛮复杂的 
//string date = "二零一二年二月二一日"; 
string strNumb = "一1 二2 三3 四4 五5 六6 七7 八8 九9 零0" ; 
string [] strNumbs = strNumb.Split(' ' ); 
string nullYear = "" ; 
Dictionary <char , char> years = new Dictionary < char, char >(); 
for (int i = 0; i < strNumbs.Length; i++) { 
years.Add(strNumbs[i][0], strNumbs[i][1]); 

//years.Add('年', '-'); 
//years.Add('月', '-'); 
for (int i = 0; i < date.Length; i++) { 
if (years.ContainsKey(date[i])) { 
nullYear += years[date[i]]; 
} else if (date[i] == '年' || date[i] == '月' ) { 
nullYear += '-' ; 
} else if (date[i] == '十' && years.ContainsKey(date[i + 1]) && !years.ContainsKey(date[i - 1])) { 
nullYear += '1' ; 
} else if (date[i] == '十' && !years.ContainsKey(date[i + 1]) && years.ContainsKey(date[i - 1])) { 
nullYear += '0' ; 
} else if (date[i] == '十' && !years.ContainsKey(date[i + 1]) && !years.ContainsKey(date[i - 1])) { 
nullYear += "10" ; 


Console .WriteLine(nullYear); 
Console .ReadKey(); 



e、泛型集合练习2中日期转换提取为方法 

复制代码代码如下:


View Code 
using System; 
using System.Collections.Generic; 
namespace _07_日期Change { 
class Program { 
static string str = "一1 二2 三3 四4 五5 六6 七7 八8 九9 零0" ; 
static string [] strs = str.Split( ' '); 
static Dictionary < char, char > money = new Dictionary< char , char >(); 
static void Main( string[] args) { 
for (int i = 0; i < strs.Length; i++) { 
money.Add(strs[i][0], strs[i][1]); 

//string date = "二零一二年二月二一日"; 
string date = "二零一二年二十月二十一日" ; 
date = newstr(date); 
string nullYear = "" ; 
//money.Add('年', '-'); 
//money.Add('月', '-'); 
for (int i = 0; i < date.Length; i++) { 
if (money.ContainsKey(date[i])) { 
nullYear += money[date[i]]; 
} else if (date[i] == '年' || date[i] == '月' ) { 
nullYear += '-' ; 


Console .WriteLine(nullYear); 
Console .WriteLine(date);//二零一二年一二月二一日 
Console .ReadKey(); 

//十左右字符都在字典中 那么十消失 如果左边不在右边在则变1 如果左边在右边不在则变0 如果左右都不在则变10 
public static string newstr( string str) { 
string newstr = "" ; 
for (int i = 0; i < str.Length; i++) { 
if (str[i] == '十' ) { 
bool left = money.ContainsKey(str[i - 1]); 
bool right = money.ContainsKey(str[i + 1]); 
if (left && right) { 
newstr += "" ; 
} else if (right) { //!left && 
newstr += "一" ; 
} else if (left) { //&& !right 
newstr += "零" ; 
} else {//if (!left && !right) 
newstr += "一零" ; 

} else { 
newstr += str[i]; 


return newstr; 



f、泛型集合练习之翻译软件 

复制代码代码如下:


View Code 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
namespace _05_翻译软件 { 
public partial class Form1 : Form { 
public Form1() { 
InitializeComponent(); 

Dictionary <string , string> dic = new Dictionary < string, string >(); 
private void btnChange_Click( object sender, EventArgs e) { 
if (dic.ContainsKey(txtEnglish.Text)) { 
txtChina.Text = dic[txtEnglish.Text]; 
} else { 
MessageBox .Show("请购买新的字典" ); 


private void Form1_Load( object sender, EventArgs e) { 
string [] filecon = File .ReadAllLines( "英汉词典TXT格式.txt" , Encoding .Default);//格式遵循abandon v.抛弃,放弃 
for (int i = 0; i < filecon.Count(); i++) { 
string [] arr = filecon[i].Split(new char[] { ' ' }, StringSplitOptions .RemoveEmptyEntries); 
if (!dic.ContainsKey(arr[0])) { 
dic.Add(arr[0], arr[1]); 





集合类List与Dictonary实例练习