C#中字典的使用Dictionary

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace 集合
  6. {
  7.     class 字典
  8.     {
  9.         public static void Main()
  10.         {
  11.             //字典也称映射或者散列表,主要特点是可以根据键快速查找值,也可以自由删除添加元素
  12.             //在删除添加时,不会像列表一样,移动之后的所有元素,产生内存的开销。
  13.             //.net中提供了几个字典,可以使用最主要的类是Dictionary<TKey,TValue>
  14.             //这个类与我们上面说的SortedList用法完全一样,这里不再多说了。
  15.             //键的类型  
  16.             //用做字典中键的类型必须重写Object类中的GetHashCode()方法,只要字典类需要确定元素的位置,就要调用本方法
  17.             //字典内部通过调用这个方法的返回值,来计算产生散列。。这个算法不做介绍 ,但我要知道,它涉及到素数
  18.             //所以字典的容量是一个素数
  19.             //GetHashCode()方法的实现需要遵循以下几点
  20.             // 1 相同的对象应总是返回相同的值
  21.             // 2 不同的对象可以返回相同的值
  22.             // 3 应执行得比较快,计算的开销不大。
  23.             // 4 不能刨出异常
  24.             // 5 应至少使用一个实例字段
  25.             // 6 散列码值应平均分布在int可以存储的整个数字区上
  26.             // 7 散列码最好在对象的生存期中不发生变化
  27.             //提示: 字典的性能取决于GetHashCode()方法的实现代码
  28.             
  29.             Dictionary<EmployeeID , Employee> emps = new Dictionary<EmployeeID,Employee>(31) ;
  30.             EmployeeID idAladdin = new EmployeeID( "C7102" ) ;
  31.             Employee aladdin = new Employee( "aladdin" , 5000.00m , idAladdin ) ;
  32.             emps.Add( idAladdin , aladdin ) ;
  33.             Console.WriteLine( aladdin ) ;
  34.             EmployeeID idjacky = new EmployeeID( "C7106" ) ;
  35.             Employee jacky = new Employee( "jacky" , 5000.00m , idjacky ) ;
  36.             emps.Add( idjacky , jacky ) ;
  37.             Console.WriteLine( jacky ) ;
  38.             EmployeeID idzhao = new EmployeeID( "C8102" ) ;
  39.             Employee zhao = new Employee( "zhao" , 5000.00m , idzhao ) ;
  40.             emps.Add( idzhao , zhao ) ;
  41.             Console.WriteLine( zhao ) ;
  42.             EmployeeID idxiaofei = new EmployeeID( "C9102" ) ;
  43.             Employee xiaofei = new Employee( "xiaofei" , 5000.00m , idxiaofei ) ;
  44.             emps.Add( idxiaofei , xiaofei ) ;
  45.             Console.WriteLine( xiaofei ) ;
  46.             EmployeeID iddabi = new EmployeeID( "C7602" ) ;
  47.             Employee dabi = new Employee( "dabi" , 5000.00m , iddabi ) ;
  48.             emps.Add( iddabi , dabi ) ;
  49.             Console.WriteLine( dabi ) ;
  50.             EmployeeID idalong = new EmployeeID( "C7302" ) ;
  51.             Employee along = new Employee( "along" , 5000.00m , idalong ) ;
  52.             emps.Add( idalong , along ) ;
  53.             Console.WriteLine( along ) ;
  54.             EmployeeID idcarl = new EmployeeID( "C7402" ) ;
  55.             Employee carl = new Employee( "carl" , 5000.00m , idcarl ) ;
  56.             emps.Add( idcarl , carl ) ;
  57.             Console.WriteLine( carl ) ;
  58.             EmployeeID idjeff = new EmployeeID( "C7502" ) ;
  59.             Employee jeff = new Employee( "jeff" , 5000.00m , idjeff ) ;
  60.             emps.Add( idjeff , jeff ) ;
  61.             Console.WriteLine( jeff ) ;
  62.             EmployeeID iddenny = new EmployeeID( "C6602" ) ;
  63.             Employee denny = new Employee( "denny" , 5000.00m , iddenny ) ;
  64.             emps.Add( iddenny , denny ) ;
  65.             Console.WriteLine( denny ) ;
  66.             EmployeeID idmatt = new EmployeeID( "C7701" ) ;
  67.             Employee matt = new Employee( "matt" , 5000.00m , idmatt ) ;
  68.             emps.Add( idmatt , matt ) ;
  69.             Console.WriteLine( matt ) ;
  70.             Console.ReadLine() ;
  71.         }
  72.     }
  73.     struct EmployeeID : IEquatable<EmployeeID>
  74.     {
  75.         private readonly char prefix ;
  76.         private readonly int number ;
  77.         public EmployeeID( string id )
  78.         {
  79.             this.prefix = (id.ToUpper())[0] ;
  80.             int numLength = id.Length - 1 ;
  81.             this.number = int.Parse( id.Substring( 1 , numLength > 6 ? 6 : numLength ) ) ;
  82.         }
  83.         public override string ToString()
  84.         {
  85.             return this.prefix.ToString() + string.Format( "{0,6:000000}" , number ) ;
  86.         }
  87.         public override int GetHashCode()
  88.         {
  89.             return ( number ^ number << 16 ) * 0x15051505 ;
  90.         }
  91.         public bool Equals( EmployeeID other )
  92.         {
  93.             return (  other.number == this.number && this.prefix == other.prefix  ) ;
  94.         }
  95.     }
  96.     class Employee
  97.     {
  98.         private string name ;
  99.         private decimal salary ;
  100.         private readonly EmployeeID id ;
  101.         public Employee( string name , decimal salary , EmployeeID id ) 
  102.         {
  103.             this.name = name ;
  104.             this.salary = salary ;
  105.             this.id = id ;
  106.         }
  107.         public override string ToString()
  108.         {
  109.             return string.Format( "{0} : {1,-20} {2:C}" , id.ToString() , name,salary ) ;
  110.         }
  111.     }
  112. }
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值