zoukankan      html  css  js  c++  java
  • C#中SortedList类的使用

    C#中SortedList类

    命名空间:System.Collections

    程序集:mscorlib(在mscorlib.dll中)

    语法:public class SortedList : IDictionary, ICollection, IEnumerable, ICloneable


    构造函数:

    1. SortedList()

    初始化 SortedList 类的新实例。该实例为空、具有默认初始容量并依据 IComparable 接口(此接口由加入到 SortedList 中的每一个键实现)进行排序

    例:

    SortedList mySL1 = new SortedList();

    2. SortedList (IComparer)

    初始化 SortedList 类的新实例,该实例为空、具有默认初始容量并依据指定的 IComparer 接口进行排序。

    依据指定的 IComparer 实现对元素进行排序。

    假设 comparer 为 空引用(在 Visual Basic 中为 Nothing),则会使用每一个键的 IComparable 实现。因此,每一个键必须实现 IComparable 接口,以便可以与 SortedList 中的其它每一个键进行比較。

    SortedList 的容量是 SortedList 能够保存的元素数。向 SortedList 加入元素时,将通过又一次分配内部数组,依据须要自己主动增大容量。

    假设能够预计集合的大小。那么当指定初始容量后,将无需在向 SortedList 中加入元素时运行大量的大小调整操作。

    此构造函数的运算复杂度为 O(1)。

    例:

    SortedList mySL2 = new SortedList(new CaseInsensitiveComparer());

    3. SortedList (IDictionary)

    初始化 SortedList 类的新实例,该实例包括从指定字典复制的元素、具有与所复制的元素数同样的初始容量并依据由每一个键实现的 IComparable 接口排序。

    每一个键必须实现 IComparable 接口。以便可以与 SortedList 中其它每一个键进行比較。

    依据加入到 SortedList 的每一个键的 IComparable 实现对元素进行排序。

    Hashtable 是能够传递到此构造函数的 IDictionary 实现的一个演示样例。新 SortedList 包括存储在 Hashtable 中的键和值的副本。

    SortedList 的容量是 SortedList 能够保存的元素数。向 SortedList 加入元素时,将通过又一次分配内部数组,依据须要自己主动增大容量。

    假设能够预计集合的大小。那么当指定初始容量后,将无需在向 SortedList 中加入元素时运行大量的大小调整操作。

    此构造函数的运算复杂度为 O(n),当中 n 是 d 中的元素数。

    例:

    Hashtable myHT = new Hashtable();

    myHT.Add("FIRST", "Hello");

    myHT.Add("SECOND", "World");

    myHT.Add("THIRD", "!");

    // Create a SortedList using the default comparer.

    SortedList mySL3 = new SortedList(myHT);

    4. SortedList (Int32)

    初始化 SortedList 类的新实例,该实例为空、具有指定的初始容量并依据 IComparable 接口(此接口由加入到 SortedList 中的每一个键实现)进行排序。

    例:

    SortedList mySL4 = new SortedList( 3 );

    Console.WriteLine("mySL1 (default):");

    mySL4.Add("FIRST", "Hello");

    mySL4.Add("SECOND", "World");

    mySL4.Add("THIRD", "!");

    5. SortedList (IComparer, Int32)

    初始化 SortedList 类的新实例,该实例为空、具有指定的初始容量并依据指定的 IComparer 接口排序。

    例:

    SortedList mySL5 = new SortedList(new CaseInsensitiveComparer(), 3);

    Console.WriteLine("mySL2 (case-insensitive comparer):");

    mySL5.Add("FIRST", "Hello");

    mySL5.Add("SECOND", "World");

    mySL5.Add("THIRD", "!");

    6. SortedList (IDictionary, IComparer)

    初始化 SortedList 类的新实例。该实例包括从指定字典复制的元素、具有与所复制的元素数同样的初始容量并依据指定的 IComparer 接口排序。

    例:

    Hashtable myHT = new Hashtable();

    myHT.Add("FIRST", "Hello");

    myHT.Add("SECOND", "World");

    myHT.Add("THIRD", "!");

    SortedList mySL6 = new SortedList(myHT, new CaseInsensitiveComparer());

    方法:

    1. 加入元素

    SortedList mySL = new SortedList();

    mySL.Add( "one", "The" );

    mySL.Add( "two", "quick" );

    mySL.Add( "three", "brown" );

    mySL.Add( "four", "fox" );

    此时,mySL.Count = 4, mySL.Capacity = 16

    2. 清空元素

    mySL.Clear();

    此时,mySL.Count = 0, mySL.Capacity = 16;

    3. 是否包括特定键(ContainsKey)、特定值(ContainsValue)

    SortedList mySL = new SortedList();

    mySL.Add( 2, "two" );

    mySL.Add( 4, "four" );

    mySL.Add( 1, "one" );

    mySL.Add( 3, "three" );

    mySL.Add( 0, "zero" );

    int myKey = 2;

    bool F1 = mySL.ContainsKey( myKey );

    String myValue = "three";

    bool F2 = mySL.ContainsValue( myValue );

    此时F1为ture,F2为true

    4. 获取键(GetByIndex)、值(GetKey)

    SortedList mySL = new SortedList();

    mySL.Add( 1.3, "fox" );

    mySL.Add( 1.4, "jumped" );

    mySL.Add( 1.5, "over" );

    mySL.Add( 1.2, "brown" );

    mySL.Add( 1.1, "quick" );

    mySL.Add( 1.0, "The" );

    mySL.Add( 1.6, "the" );

    mySL.Add( 1.8, "dog" );

    mySL.Add( 1.7, "lazy" );

    int myIndex=3;

    Console.WriteLine( "The key at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) );

    Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) );

    此时The key at index 3 is 1.3. The value at index 3 is fox.

    *************************************************************************************************

    备注

    SortedList 元素可通过其键来訪问 (如随意 IDictionary 实现中的元素)。或通过其索引来訪问(如随意 IList 实现中的元素)。

    SortedList 在内部维护两个数组以存储列表中的元素;即,一个数组用于键,还有一个数组用于相关联的值。

    每一个元素都是一个可作为 DictionaryEntry 对象进行訪问的键/值对。键不能为 空引用(在 Visual Basic 中为 Nothing)。但值能够。

    SortedList 的容量是 SortedList 能够保存的元素数。

    SortedList 的默认初始容量为 0。

    随着元素加入到 SortedList 中,在须要时能够通过又一次分配自己主动添加容量。

    可通过调用 TrimToSize 或通过显式设置 Capacity 属性降低容量。

    SortedList 的元素将依照特定的 IComparer 实现(在创建 SortedList 时指定)或依照键本身提供的 IComparable 实现并根据键来进行排序。

    不论在哪种情况下,SortedList 都不同意反复键。

    索引顺序基于排序顺序。

    当加入元素时,元素将按正确的排序顺序插入 SortedList。同一时候索引会对应地进行调整。当移除元素时。索引也会对应地进行调整。因此,当在 SortedList 中加入或移除元素时。特定键/值对的索引可能会更改。

    因为要进行排序。所以在 SortedList 上操作比在 Hashtable 上操作要慢。可是。SortedList 同意通过相关联键或通过索引对值进行訪问,可提供更大的灵活性。 可使用一个整数索引訪问此集合中的元素。

    此集合中的索引从零開始。

    C# 语言中的 foreach 语句(在 Visual Basic 中为 for each)须要集合中每一个元素的类型。

    因为 SortedList 的每一个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 DictionaryEntry 类型。



  • 相关阅读:
    解决流氓软件的工具,做个记录
    目前使用较好的网盘搜索引擎
    网页截图分段保存
    国外统计学课程主页Statistical Books, Manuals and Journals
    notepad++ TextFX替代
    只用 4 个月打造机器学习必备技能,这位工程师成功翻转职涯人生
    时序差分学习
    来自NVIDIA开源的pix2pixHD,将Image-to-Image Translation带到了另一个境界
    关于HTML5,最牛逼的10本书!
    IDEA配置和插件
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6785867.html
Copyright © 2011-2022 走看看