C#中字典的使用Dictionary
阿新 • • 發佈:2018-12-26
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 集合
- {
- class 字典
- {
- publicstaticvoid 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>
- {
- privatereadonlychar prefix ;
- privatereadonlyint 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 ) ) ;
- }
- publicoverridestring ToString()
- {
- returnthis.prefix.ToString() + string.Format( "{0,6:000000}" , number ) ;
- }
- publicoverrideint GetHashCode()
- {
- return ( number ^ number << 16 ) * 0x15051505 ;
- }
- publicbool Equals( EmployeeID other )
- {
- return ( other.number == this.number && this.prefix == other.prefix ) ;
- }
- }
- class Employee
- {
- privatestring name ;
- privatedecimal salary ;
- privatereadonly EmployeeID id ;
- public Employee( string name , decimal salary , EmployeeID id )
- {
- this.name = name ;
- this.salary = salary ;
- this.id = id ;
- }
- publicoverridestring ToString()
- {
- returnstring.Format( "{0} : {1,-20} {2:C}" , id.ToString() , name,salary ) ;
- }
- }
- }