C#学习日记 字典
来源:互联网 发布:cf积分刷点卷软件 编辑:程序博客网 时间:2024/06/05 00:09
字典能根据键快速查找值,也可以自由的添加元素和删除元素。键会转换成一个散列,利用散列创建一个数字,它将索引和值关联起来。索引包含一个到到值得链接。键必须重写GetHashCode()方法。Equals()方法必须重写GetHashCode(),不然编译会出错。字典常用字符串做键。
using System;using System.Collections.Generic;namespace Sample{public class MainEntryPoint{static int Main(string[] args){//容量一般设置为素数 如果设置的不是//类会使用传递给构造函数整数的最近一个素数var employees = new Dictionary<EmployeeId, Employee>(31);var julyId = new EmployeeId("C3755");var july = new Employee("July",379025.00m,julyId);employees.Add( julyId, july);Console.WriteLine(july);var joynId = new EmployeeId("F3547");var joyn = new Employee("Joyn",403466.00m,joynId);employees.Add(joynId, joyn);Console.WriteLine(joyn);var johnId = new EmployeeId("C3386");var john = new Employee("John",415216.00m,johnId);//通过索引把值传递进字典employees[johnId] = john;Console.WriteLine(john);Console.WriteLine();while(true){Console.Write("Enter employee id(X to exit)>");var userInput = Console.ReadLine();userInput = userInput.ToUpper();if(userInput == "X")break;EmployeeId id;try{id = new EmployeeId(userInput);Employee employee;//先判断是否有对应这个键的值//返回布尔类型 还有一个out参数带回这个值if(!employees.TryGetValue(id, out employee)){Console.WriteLine("Employee with id {0} does not exist",id);}else{Console.WriteLine(employee);}}catch (EmployeeIdException ex){Console.WriteLine(ex.Message);}}return 0;}}[Serializable]public class EmployeeIdException : Exception{public EmployeeIdException(string message) : base(message){}}[Serializable]public struct EmployeeId :IEquatable<EmployeeId>{private readonly char prefix;private readonly int number;public EmployeeId(string id){prefix = (id.ToUpper())[0];int numLength = id.Length - 1;try{number = int.Parse(id.Substring( 1, numLength > 6 ? 6 : numLength));}catch (FormatException){throw new EmployeeIdException("Invalid EmployeeId format");}}public override string ToString(){return prefix.ToString()+string.Format("{0:6:000000}", number);}//把数值平均的分布在整型范围内public override int GetHashCode(){return (number ^ number << 16) * 0x15051505;}public bool Equals(EmployeeId other){if(other == null)return false;elsereturn (prefix == other.prefix && number == other.number);}public override bool Equals(object obj){return Equals((EmployeeId)obj);}public static bool operator ==(EmployeeId left, EmployeeId right){return left.Equals(right);}public static bool operator !=(EmployeeId left, EmployeeId right){return !(left == right);}}[Serializable]public class Employee{private string name;private decimal salary;private EmployeeId id;public Employee(string name, decimal salary, EmployeeId id){this.name = name;this.salary = salary;this.id = id;}public override string ToString(){return string.Format("{0}: {1,-20} {2 : C}",id.ToString(), name, salary);}}}
0 0
- C#学习日记 字典
- [oc学习日记]字典
- OC学习日记09-字典
- c# 工作学习日记
- C#学习日记
- C#学习日记1
- C#学习日记 链表
- C#学习日记 StringBuilder
- C#学习日记 指针
- 算法学习日记 字典法排序
- c# 委托的学习日记
- CLR via C#学习日记
- c# 委托的学习日记
- C#学习日记03---数据类型
- C#学习日记27----属性
- C#学习日记 委托 事件
- C#学习日记 队列 栈
- C#学习日记 BitArray BitVector32
- 剑指offer-25 二叉树中和为某一值的路径
- 如何查看某个端口被谁占用
- Path Sum III
- 初学者的MySQL 1067 错误解决方法
- 【Dubbo】dubbo连接zookeeper注册中心因为断网导致线程无限等待问题
- C#学习日记 字典
- Java SE、Java EE、Java ME三者的区别
- 概率计算 蓝桥杯 C语言
- HTML5+CSS3整体回顾
- markdown入门
- LaTeX字体设置
- $dispaly()、$strobe()、$monitor() 、$fwrite()與blocking / nonblocking的關係 (SOC) (Verilog) (Debussy) (Verd
- maven通过tomcat插件,如何运行Java Web项目 支持断点
- 51单片机为什么无法下载程序?