(28.218.81608.81609.81613.83749.83751.83971)
实验三 泛型与集合类 班级: 16计科2 学号:2015329620062 姓名: 孙远昭 一、实验目的 1. 掌握常用非泛型集合类和集合类的使用; 2. 掌握可空类型的使用 3. 复习字符串的使用。 二、实验内容 (实验过程中编写的程序复制到本文件中,下课整理后上交到yvsou.com,上交的附件以“学号-姓名.doc”命名。如对C#或.NET Framework相关内容不熟悉,可在VS中按F1打开MSDN文档。) (写代码技巧:键入cw或for等文字,再连续键入两次Tab,会自动补全所需代码。这种功能叫“代码片段”,可在菜单“工具”“代码片段管理器”并选择CSharp语言后看到所有默认支持的代码片段。) 1. 创建一个集合类Stuff,它是下述Employee类的集合,该集合中的项可以通过一个字符串索引符来访问,该字符串索引符是人名,与Employee.Name属性相同。并给Stuff添加方法GetOldest()方法,取得其中年纪最大的员工。 提示: 1) 可模仿教材P224-225页程序来编写; 2) 或者在Stuff中添加泛型集合类。(P271) 3) DateTime类型已实现IComparable接口,可调用其中方法进行比较。 public class Employee { public string Name { get; set; } public int Salary { get; set; } public DateTime Birthday { get; set; } public override string ToString() { return string.Format("Employee({0} : Salary = {1}; Birthday = {2:D})", Name, Salary, Birthday); } } 在Main方法中测试该集合类。 public static void Main(string[] argv) { StuffDict stuff = new StuffDict(); stuff.Add(new Employee { Name = "Damon", Salary = 10, Birthday = new DateTime(1990, 5, 1) }); stuff.Add(new Employee { Name = "Niki", Salary = 15, Birthday = new DateTime(1995, 10, 4) }); stuff.Add(new Employee { Name = "Ayrton", Salary = 12, Birthday = new DateTime(1992, 6, 23) }); stuff.Add(new Employee { Name = "Graham", Salary = 13, Birthday = new DateTime(1994, 9, 15) }); Console.WriteLine(stuff["Niki"]); Console.WriteLine(stuff.GetOldest()); } 应输出: Employee(Niki : Salary = 15; Birthday = 1995年10月4日) Employee(Damon : Salary = 10; Birthday = 1990年5月1日) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class Employee { public string Name { get; set; } public int? Salary { get; set; } public DateTime? Birthday { get; set; } public Employee(string name) { Name = name; } public Employee(string name,int a,DateTime bir) { Name = name; Salary = a; Birthday = bir; } public override string ToString() { if(Salary != null)return string.Format("Employee({0} : Salary = {1}; Birthday = {2:D})", Name, Salary, Birthday); else return string.Format("Employee({0})", Name); } } class Stuff : DictionaryBase { public void Add(Employee t ) { string name = t.Name; Dictionary.Add(name, t); } public void remove(string name) { Dictionary.Remove(name); } public Employee this[string name] { get { return (Employee)Dictionary[name]; } set { Dictionary[name] = value; } } public Employee GetOldest() { Employee maxn = null; foreach (Employee t in Dictionary.Values) { if (maxn == null) { maxn = t; } if (t.Birthday < maxn.Birthday) { maxn = t; } } return maxn; } } class Program { static void Main(string[] args) { StuffDict stuff = new StuffDict(); stuff.Add(new Employee { Name = "Damon", Salary = 10, Birthday = new DateTime(1990, 5, 1) }); stuff.Add(new Employee { Name = "Niki", Salary = 15, Birthday = new DateTime(1995, 10, 4) }); stuff.Add(new Employee { Name = "Ayrton", Salary = 12, Birthday = new DateTime(1992, 6, 23) }); stuff.Add(new Employee { Name = "Graham", Salary = 13, Birthday = new DateTime(1994, 9, 15) }); Console.WriteLine(stuff["Niki"]); Console.WriteLine(stuff.GetOldest()); while (true) ; } } } 2. 改写第1题的Employee类。假设采集到的员工信息,一定包含有名字,但不一定包含工资与生日信息。请完成: 1) 用可空类型替换原工资属性、生日属性; 2) 生成两个构造函数,一个接受唯一参数(名字),一个接受三个参数。 3) 改造ToString方法,如果没有工资或生日信息,就不要输出了。 在Main方法中运行以下程序: Employee emp1 = new Employee("张三"); Console.WriteLine(emp1); Employee emp2 = new Employee("李四", 3, new DateTime(2000, 1, 15)); Console.WriteLine(emp2); 获得以下输出: Employee(张三) Employee(李四 : Salary = 3; Birthday = 2000年1月15日) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class Employee { public string Name { get; set; } public int? Salary { get; set; } public DateTime? Birthday { get; set; } public Employee(string name) { Name = name; } public Employee(string name,int a,DateTime bir) { Name = name; Salary = a; Birthday = bir; } public override string ToString() { if(Salary != null)return string.Format("Employee({0} : Salary = {1}; Birthday = {2:D})", Name, Salary, Birthday); else return string.Format("Employee({0})", Name); } } class Stuff : DictionaryBase { public void Add(Employee t ) { string name = t.Name; Dictionary.Add(name, t); } public void remove(string name) { Dictionary.Remove(name); } public Employee this[string name] { get { return (Employee)Dictionary[name]; } set { Dictionary[name] = value; } } public Employee GetOldest() { Employee maxn = null; foreach (Employee t in Dictionary.Values) { if (maxn == null) { maxn = t; } if (t.Birthday < maxn.Birthday) { maxn = t; } } return maxn; } } class Program { static void Main(string[] args) { Employee emp1 = new Employee("张?三¨y"); Console.WriteLine(emp1); Employee emp2 = new Employee("李¤?四?", 3, new DateTime(2000, 1, 15)); Console.WriteLine(emp2); while (true) ; } } } 3. 练习非泛型类ArrayList或泛型类List<T>的使用 1)随机产生 20 个随机整数(10以内的),放入一个ArrayList中,遍历这个ArrayList并输出 2)删除其中为 5 的数 3)再产生 3 个整数,插入到位置 4 处 4)把所有值为 1 的数都变成 10 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class Employee { public string Name { get; set; } public int? Salary { get; set; } public DateTime? Birthday { get; set; } public Employee(string name) { Name = name; } public Employee(string name,int a,DateTime bir) { Name = name; Salary = a; Birthday = bir; } public override string ToString() { if(Salary != null)return string.Format("Employee({0} : Salary = {1}; Birthday = {2:D})", Name, Salary, Birthday); else return string.Format("Employee({0})", Name); } } class Stuff : DictionaryBase { public void Add(Employee t ) { string name = t.Name; Dictionary.Add(name, t); } public void remove(string name) { Dictionary.Remove(name); } public Employee this[string name] { get { return (Employee)Dictionary[name]; } set { Dictionary[name] = value; } } public Employee GetOldest() { Employee maxn = null; foreach (Employee t in Dictionary.Values) { if (maxn == null) { maxn = t; } if (t.Birthday < maxn.Birthday) { maxn = t; } } return maxn; } } class Program { static void Main(string[] args) { ArrayList vec = new ArrayList(); Random rd = new Random(); for (int i = 0; i < 20; i++) { vec.Add(rd.Next(10)); } for (int i = 0; i < vec.Count; i++) { Console.WriteLine(vec[i]); if ((int)vec[i] == 5) vec.RemoveAt(i); } //vec.Remove(5); for(int i=0;i<3;i++) { vec.Insert(4, rd.Next(10)); } for (int i = 0; i < vec.Count; i++) { if ((int)vec[i] == 1) vec[i] = 10; } Console.WriteLine("经-过y程¨¬序¨°运?行D后¨®数ºy组Á¨¦情¨¦况?:êo"); for (int i = 0; i < vec.Count; i++) { Console.WriteLine(" "+ vec[i]); } while (true) ; } } } 4. 练习非泛型类Dictionary或泛型类Dictionary<K,V>的使用 对上一题的源代码复制到“记事本”程序中,将其中的双引号替换为两个双引号,再复制到程序中,写成string str = @”复制的代码放这里”,形成一个字符串。 声明一个字符串数组,例如: string[] keys = { "Console", "static", "foreach", "int", "if" };//其中内容可任选 统计这些字符串在代码中出现的次数,存入Dictionary实例,并输出。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string str = @"using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class Employee { public string Name { get; set; } public int? Salary { get; set; } public DateTime? Birthday { get; set; } public Employee(string name) { Name = name; } public Employee(string name,int a,DateTime bir) { Name = name; Salary = a; Birthday = bir; } public override string ToString() { if(Salary != null)return string.Format(""Employee({0} : Salary = {1}; Birthday = {2:D})"", Name, Salary, Birthday); else return string.Format(""Employee({0})"", Name); } } class Stuff : DictionaryBase { public void Add(Employee t ) { string name = t.Name; Dictionary.Add(name, t); } public void remove(string name) { Dictionary.Remove(name); } public Employee this[string name] { get { return (Employee)Dictionary[name]; } set { Dictionary[name] = value; } } public Employee GetOldest() { Employee maxn = null; foreach (Employee t in Dictionary.Values) { if (maxn == null) { maxn = t; } if (t.Birthday < maxn.Birthday) { maxn = t; } } return maxn; } } class Program { static void Main(string[] args) { ArrayList vec = new ArrayList(); Random rd = new Random(); for (int i = 0; i < 20; i++) { vec.Add(rd.Next(10)); } for (int i = 0; i < vec.Count; i++) { Console.WriteLine(vec[i]); if ((int)vec[i] == 5) vec.RemoveAt(i); } //vec.Remove(5); for(int i=0;i<3;i++) { vec.Insert(4, rd.Next(10)); } for (int i = 0; i < vec.Count; i++) { if ((int)vec[i] == 1) vec[i] = 10; } Console.WriteLine(""经-过y程¨¬序¨°运?行D后¨®数ºy组Á¨¦情¨¦况?:êo""); for (int i = 0; i < vec.Count; i++) { Console.WriteLine("" ""+ vec[i]); } while (true) ; } } } "; string[] keys = { "Console", "static", "foreach", "int", "if" }; Dictionary<string, int> vec = new Dictionary<string, int>(); for (int i = 0; i < keys.Length; i++) { int pos = -1; int num = 0; while (true) { pos = str.IndexOf(keys[i], pos + 1); //Console.WriteLine(pos); if (pos == -1) break; num++; } vec[keys[i]] = num; //Console.WriteLine("...."+num); } for (int i = 0; i < vec.Count; i++) { Console.WriteLine(keys[i]+"在¨²字Á?符¤?串ä?中D的Ì?个?数ºy为a:êo"+vec[keys[i]]); } while (true) ; } } } 三、实验心得与体会
group status
👤 group joined: 0 ⏳ group pending: 0 🚫 group blocked: 0







