- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 集合
- {
- class 字典
- {
- public static void Main()
- {
- //字典也称映射或者散列表,主要特点是可以根据键快速查找值,也可以自由删除添加元素
- //在删除添加时,不会像列表一样,移动之后的所有元素,产生内存的开销。
- //.net中提供了几个字典,可以使用最主要的类是Dictionary<TKey,TValue>
- //这个类与我们上面说的SortedList用法完全一样,这里不再多说了。
- //键的类型
- //用做字典中键的类型必须重写Object类中的GetHashCode()方法,只要字典类需要确定元素的位置,就要调用本方法
- //字典内部通过调用这个方法的返回值,来计算产生散列。。这个算法不做介绍 ,但我要知道,它涉及到素数
- //所以字典的容量是一个素数
- //GetHashCode()方法的实现需要遵循以下几点
- // 1 相同的对象应总是返回相同的值
- // 2 不同的对象可以返回相同的值
- // 3 应执行得比较快,计算的开销不大。
- // 4 不能刨出异常
- // 5 应至少使用一个实例字段
- // 6 散列码值应平均分布在int可以存储的整个数字区上
- // 7 散列码最好在对象的生存期中不发生变化
- //提示: 字典的性能取决于GetHashCode()方法的实现代码
- Dictionary<EmployeeID , Employee> emps = new Dictionary<EmployeeID,Employee>(31) ;
- EmployeeID idAladdin = new EmployeeID( "C7102" ) ;
- Employee aladdin = new Employee( "aladdin" , 5000.00m , idAladdin ) ;
- emps.Add( idAladdin , aladdin ) ;
- Console.WriteLine( aladdin ) ;
- EmployeeID idjacky = new EmployeeID( "C7106" ) ;
- Employee jacky = new Employee( "jacky" , 5000.00m , idjacky ) ;
- emps.Add( idjacky , jacky ) ;
- Console.WriteLine( jacky ) ;
- EmployeeID idzhao = new EmployeeID( "C8102" ) ;
- Employee zhao = new Employee( "zhao" , 5000.00m , idzhao ) ;
- emps.Add( idzhao , zhao ) ;
- Console.WriteLine( zhao ) ;
- EmployeeID idxiaofei = new EmployeeID( "C9102" ) ;
- Employee xiaofei = new Employee( "xiaofei" , 5000.00m , idxiaofei ) ;
- emps.Add( idxiaofei , xiaofei ) ;
- Console.WriteLine( xiaofei ) ;
- EmployeeID iddabi = new EmployeeID( "C7602" ) ;
- Employee dabi = new Employee( "dabi" , 5000.00m , iddabi ) ;
- emps.Add( iddabi , dabi ) ;
- Console.WriteLine( dabi ) ;
- EmployeeID idalong = new EmployeeID( "C7302" ) ;
- Employee along = new Employee( "along" , 5000.00m , idalong ) ;
- emps.Add( idalong , along ) ;
- Console.WriteLine( along ) ;
- EmployeeID idcarl = new EmployeeID( "C7402" ) ;
- Employee carl = new Employee( "carl" , 5000.00m , idcarl ) ;
- emps.Add( idcarl , carl ) ;
- Console.WriteLine( carl ) ;
- EmployeeID idjeff = new EmployeeID( "C7502" ) ;
- Employee jeff = new Employee( "jeff" , 5000.00m , idjeff ) ;
- emps.Add( idjeff , jeff ) ;
- Console.WriteLine( jeff ) ;
- EmployeeID iddenny = new EmployeeID( "C6602" ) ;
- Employee denny = new Employee( "denny" , 5000.00m , iddenny ) ;
- emps.Add( iddenny , denny ) ;
- Console.WriteLine( denny ) ;
- EmployeeID idmatt = new EmployeeID( "C7701" ) ;
- Employee matt = new Employee( "matt" , 5000.00m , idmatt ) ;
- emps.Add( idmatt , matt ) ;
- Console.WriteLine( matt ) ;
- Console.ReadLine() ;
- }
- }
- struct EmployeeID : IEquatable<EmployeeID>
- {
- private readonly char prefix ;
- private readonly int number ;
- public EmployeeID( string id )
- {
- this.prefix = (id.ToUpper())[0] ;
- int numLength = id.Length - 1 ;
- this.number = int.Parse( id.Substring( 1 , numLength > 6 ? 6 : numLength ) ) ;
- }
- public override string ToString()
- {
- return this.prefix.ToString() + string.Format( "{0,6:000000}" , number ) ;
- }
- public override int GetHashCode()
- {
- return ( number ^ number << 16 ) * 0x15051505 ;
- }
- public bool Equals( EmployeeID other )
- {
- return ( other.number == this.number && this.prefix == other.prefix ) ;
- }
- }
- class Employee
- {
- private string name ;
- private decimal salary ;
- private readonly 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 ) ;
- }
- }
- }
C#中字典的使用Dictionary
最新推荐文章于 2024-06-03 19:27:03 发布