C#学习笔记(十九):字典
自定义泛型
泛型类,第一替代符T,第二替代符U
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace m1w4d5_dictionary { class Program { static void Main(string[] args) { //把数字转换成中文字符 string inputStr = Console.ReadLine(); //123456789 //一二三四五 //字典<key,value> //声明变量 Dictionary<char, string> dic = new Dictionary<char, string>(); dic.Add('1', "一"); dic['2'] = "二"; dic['3'] = "三"; //修改一个元素 //dic.Add('1',"壹");位置已占,会报错 dic['2'] = "贰"; //访问元素 //Console.WriteLine(dic['2']); //遍历 foreach (var item in dic) { //Console.WriteLine(item); if (item.Key == '1') { Console.WriteLine(item.Value); } } Console.WriteLine(); Dictionary<char, char> dic1 = new Dictionary<char, char>(); string num = "1234567890"; string str = "一二三四五六七八九零"; for (int i = 0; i < num.Length; i++) { dic1[num[i]] = str[i]; } for (int i = 0; i < inputStr.Length; i++) { //如果用户输入的key不在我们的范围内,会报错 //通过ContainsKey判定字典中是否包含了对应的key if (dic1.ContainsKey(inputStr[i])) { Console.Write(dic1[inputStr[i]]); } } Console.WriteLine(); //计算字符串中每个字母出现的次数"Welcome to China! Welcome to HangKang!" //我能通过字母(char)找到他出现的次数(int) Dictionary<char, int> dic2 = new Dictionary<char, int>(); string str1 = "Welcome to China! Welcome to HangKang!"; for (int i = 0; i < str1.Length; i++) { //如果你在字典有了,字典中有了这个字母 if (dic2.ContainsKey(str1[i])) { //你的值就自增 dic2[str1[i]]++; } else { //你的值就是 dic2[str1[i]] = 1; } } foreach (var item in dic2) { Console.WriteLine(item.Key +":"+item.Value); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 泛型 { //定义 //泛型是C#的一种特性,可以让我们将一种结构(类,接口)或者一种逻辑(函数)应用到所有类型 //如果我们要把一个类型或者方法写成泛型的,我们只需要在名称后面加上<>,<>中可以填入若干个类型替代符 class MyList<T> { T[] data; public T this[int index] { get { if (index < 0 || index >= count) { throw new OutOfMemoryException(); } return data[index]; } set { if (index < 0 || index >= count) { throw new OutOfMemoryException(); } else { data[index] = value; } } } public int Capacity { get { return data.Length; } set { if (value == count) return; if (value < count) value = count; //建立新数组 T[] newData = new T[value]; //拷贝数据 for (int i = 0; i < data.Length; i++) newData[i] = data[i]; //用新数组换老数组 data = newData; } } int count = 0; public int Count => count; //添加 public MyList(int capacity = 4) { data = new T[capacity]; } //动态增长 public void Add(T item) { if (count >= Capacity) Capacity *= 2; data[count] = item; count++; } //删除 public void Remove(T item) { int index = IndexOf(item); if (index != -1) { RemoveAt(index); } } public void RemoveLast(T item) { int index = LastIndexOf(item); if (index != -1) { RemoveAt(index); } } public void RemoveAt(int index) { if (index >= count) { throw new IndexOutOfRangeException(); } for (int i = index + 1; i < count; i++) { data[i - 1] = data[i]; } count--; } public void RemoveAll(T item) { while (true) { int index = IndexOf(item); if (index != -1) { RemoveAt(index); } else { break; } } } //查找 public int IndexOf(T item) { int index = -1; for (int i = 0; i < count; i++) { if (item.Equals(data[i])) { return i; } } return index; } public int LastIndexOf(T item) { int index = -1; for (int i = count - 1; i >= 0; i--) { if (item.Equals(data[i])) { return i; } } return index; } public void Sort(Comparison<T> comparison) { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { if (comparison(data[j], data[j + 1]) > 0) { T temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; } } } } public void Sort(IComparer<T> iCompareer) { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { if (iCompareer.Compare(data[j], data[j + 1]) > 0) { T temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; } } } } public void Sort() { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { IComparable<T> Icompar = data[j] as IComparable<T>; if (Icompar.CompareTo(data[j + 1]) > 0) { T temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; } } } } } class Monster : IComparable<Monster> { public Monster(string name, int attack, int defend, int health) { this.name = name; this.attack = attack; this.defend = defend; this.health = health; } public string name; public int attack; public int defend; public int health; public override string ToString() { return string.Format("{0}:[attack:{1},defend:{2},health{3}]", name, attack, defend, health); } public int CompareTo(Monster other) { //比较某一个参数,返回对应值 //如果大就返回大于0的数 自已排在对比参数的后面 //如果小就返回小于0的数 自已排在对比参数的前面 //如果相等就返回0 不换 //这样在外部调用Sort的时候会 形成一个以这个参数为标准的升序排序 return attack - other.attack; } public static int AttackSort(Monster a, Monster b) { return a.attack - b.attack; } public static int DefendSort(Monster a, Monster b) { return a.defend - b.defend; } } class Program { static void Main(string[] args) { //List<int> list = new List<int>(); //list.Add(5); //list.Add(6); //list.Add(7); //list.Capacity = 0; //Console.WriteLine(list.Capacity); MyList<Monster> myList = new MyList<Monster>(); Random roll = new Random(); //Console.WriteLine(myList.Capacity); for (int i = 0; i < 10; i++) { myList.Add(new Monster(i.ToString(), roll.Next(5, 10), roll.Next(10, 15), roll.Next(100, 150))); } //myList[5] = 1; //myList[7] = 1; myList.Sort(); for (int i = 0; i < myList.Count; i++) { Console.WriteLine(myList[i]); } } } }