C#中SortedList类的使用
来源:互联网 发布:淘宝仿天猫模板免费 编辑:程序博客网 时间:2024/05/06 04:16
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 = 162. 清空元素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为true4. 获取键(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 ) );
构造函数: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 = 162. 清空元素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为true4. 获取键(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 类型。 0 0
- C#中SortedList类的使用
- c# SortedList的使用
- C# 中使用有序表SortedList
- C# SortedList类介绍
- 使用SortedList类建立排序的DropDownList
- SortedList的使用举例
- C# SortedList
- C#中SortedList 和 SortedDictionary集合类型
- c# SortedList的妙用 (GroupBy)
- C# 集合类(五):SortedList
- C# 集合类(五):SortedList
- C# 集合类(五):SortedList
- C#集合类ArrayList HashTable SortedList Dictionary
- SortedList 类
- SortedList类
- SortedList 类
- C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)
- C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类) 收藏
- uva是崩了 吗,还是我太年轻?
- 如何让人的一生价值最大
- [XCode] Interface Builder 是怎么工作的
- action的变量在jsp中的取值问题
- 144.Binary Tree Preorder Traversal(二叉树先序遍历)
- C#中SortedList类的使用
- iOS中多控制器管理 UITabBarController
- hdu 4786 Fibonacci Tree(最小生成树)
- Cocos2D
- eclipse中启动项目报内存溢出问题通过修改配置解决
- 创建表空间
- 【HttpClient4.5实训】一.HttpClient4.5模拟浏览器GET请求访问新浪网(非原文教程)
- 数据库 - 第三范式(3NF)
- Json对象与Json字符串互转(4种转换方式)