C# 中使用有序表SortedList

  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.             //如果要用排好序的表,可以使用SortedList<TKey,TValue>来给元素排序
  12.             SortedList<string , string > books = new SortedList<string,string>() ;
  13.            
  14.             books.Add( "aladdin" , "64kb@163.com" ) ;
  15.             books.Add( "zhaohaifu" , "65kb@163.com" ) ;
  16.             books.Add( "jacky" , "66kb@163.com" ) ;
  17.             books.Add( "fuck" , "67kb@163.com" ) ;
  18.             //键是不允许重复的,下面我们用Add方法重新添加一次aladdin
  19.             //books.Add( "aladdin" , "sdaf" ) ; //刨出异常
  20.             //但如果使用索引来赋值,如果键存在,则覆盖,如果不存在,相当于ADD添加。
  21.             books[ "aladdin" ] = "haha_new" ;
  22.             foreachstring str in books.Keys )
  23.             {
  24.                 Console.WriteLine( str ) ;
  25.             }
  26.             foreachstring str in books.Values )
  27.             {
  28.                 Console.WriteLine( str ) ;
  29.             }
  30.             //一次性遍历值键 
  31.             foreach( KeyValuePair<string,string> book in books )
  32.             {
  33.                 Console.WriteLine( "名字:{0}邮箱:{1}" , book.Key , book.Value ) ;
  34.             }
  35.             //从上面结果可以看出,aladdin被替换了,他的值变成了haha_new
  36.             //下面简单介绍一下SortedList<TKey,TValue>中的方法与属性
  37.             //Capacity  这个属性用来设置与得到有序表的容量,与Ilist一样,也是成倍增长的
  38.             //Comparer 返回与有序表相关的比较器,可以从构造中传入该比较器
  39.             // Remove() RemoveAt()  按键删除与按索引删除
  40.             // ContainsKey() ; ContainsValue() ; 检查是不是有包含指定值的键,或者值
  41.             // TryGetValue() ; 尝试获得指定键的值,如果有就是true 并用out把值带回来,没有就是flase
  42.             //按键删除
  43.             Console.WriteLine(  "-------------------------按键删除--------------" ) ;
  44.             books.Remove( "aladdin" ) ;
  45.             foreach( KeyValuePair<string,string> book in books )
  46.             {
  47.                 Console.WriteLine( "名字:{0}邮箱:{1}" , book.Key , book.Value ) ;
  48.             }
  49.             //可以看出aladdin被删除了
  50.             //下面我们按索引删除
  51.             books.RemoveAt( 0 ) ;
  52.             Console.WriteLine(  "-------------------------按索引删除--------------" ) ;
  53.             foreach( KeyValuePair<string,string> book in books )
  54.             {
  55.                 Console.WriteLine( "名字:{0}邮箱:{1}" , book.Key , book.Value ) ;
  56.             }
  57.             //结果中看出fuck被删除,证明删除索引是以插入顺序为准,不是排序后的顺序。
  58.             //检查是否包含aladdin 检查是否包含zhaohaifu (键)
  59.             Console.WriteLine( "是否包含aladdin键:{0}" , books.ContainsKey( "aladdin" ) ) ; // false 
  60.             Console.WriteLine( "是否包含zhaohaifu键:{0}" , books.ContainsKey( "zhaohaifu" ) ) ; // true 
  61.             //检查是否包含64kb@163.com 检查是否包含64kb@163.com (值)
  62.             Console.WriteLine( "是否包含aladdin键:{0}" , books.ContainsValue( "64kb@163.com" ) ) ; // false 
  63.             Console.WriteLine( "是否包含zhaohaifu键:{0}" , books.ContainsValue( "65kb@163.com" ) ) ; // true 
  64.             //此处的索引值是按排序后的顺序
  65.             int keyindex = books.IndexOfKey( "zhaohaifu" ) ;
  66.             Console.WriteLine( "zhaohaifu索引值:{0}" , keyindex ) ;
  67.             string value = "" ;
  68.             if( books.TryGetValue( "aladdin" , out value ))
  69.             {
  70.                 Console.WriteLine( "得到了aladdin的值 :{0}" , value ) ;
  71.             }
  72.             string value2 = "" ;
  73.             if( books.TryGetValue( "zhaohaifu" , out value2 ))
  74.             {
  75.                 Console.WriteLine( "得到了zhoahaifu的值 :{0}" , value2 ) ;
  76.             }
  77.             Console.ReadLine() ;
  78.             //注:本类是SortedList的泛型片,与之相对应的对象版的同名类存在,注意使用
  79.         }
  80.     }
  81. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值