C产品在市场上有哪些独特优势?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1115个文字,预计阅读时间需要5分钟。
1. SortedDictionary泛型类+SortedDictionary泛型类是具有O(logn)搜索运算复杂度的二叉搜索树,其中n是字典中元素的数量。这一点与SortedList泛型类相似。这两个类都具有类似的对象模型。
1、SortedDictionary泛型类
SortedDictionary 泛型类是检索运算复杂度为 O(logn) 的二叉搜索树,其中n是字典中的元素数。就这一点而言,它与SortedList泛型类相似。这两个类具有相似的对象模型,并且都具有 O(logn) 的检索运算复杂度。这两个类的区别在于内存的使用以及插入和移除元素的速度:
- SortedList使用的内存比SortedDictionary少。
- SortedDictionary可对未排序的数据执行更快的插入和移除操作:它的时间复杂度为 O(logn),而SortedList为 O(n)。
- 如果使用排序数据一次性填充列表,则SortedList比SortedDictionary快。
每个键/值对都可以作为KeyValuePair结构进行检索,或作为DictionaryEntry通过非泛型IDictionary接口进行检索。
只要键用作SortedDictionary中的键,它们就必须是不可变的。SortedDictionary中的每个键必须是唯一的。键不能为空引用(在 Visual Basic 中为 Nothing),但是如果值类型TValue为引用类型,该值则可以为空。
SortedDictionary需要比较器实现来执行键比较。可以使用一个接受comparer参数的构造函数来指定IComparer泛型接口的实现;如果不指定实现,则使用默认的泛型比较器Comparer.Default。如果类型TKey实现System.IComparable泛型接口,则默认比较器使用该实现。
C# 语言的foreach语句,需要集合中每个元素的类型。由于SortedDictionary的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是KeyValuePair类型
2、要使用Dictionary集合,需要导入C#泛型命名空间
System.Collections.Generic(程序集:mscorlib)
3、Dictionary的描述
- 从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成
- 任何键都必须是唯一的
- 键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值
- Key和Value可以是任何类型(string,int,custom class 等)
4、Dictionary常用用法:以 key 的类型为 int , value的类型为string 为例
1.创建及初始化
Dictionary<int,string>myDictionary=newDictionary<int,string>();
2.添加元素
myDictionary.Add(1,"C#");
3.通过Key查找元素
if(myDictionary.ContainsKey(1)) { Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]); }
4.通过KeyValuePair遍历元素
foreach(KeyValuePair<int,string>kvp in myDictionary) ...{ Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value); }
5.仅遍历键 Keys 属性
Dictionary<int,string>.KeyCollection keyCol=myDictionary.Keys; foreach(intkeyinkeyCol) ...{ Console.WriteLine("Key = {0}", key); }
6.仅遍历值 Valus属性
Dictionary<int,string>.ValueCollection valueCol=myDictionary.Values; foreach(stringvalueinvalueCol) ...{ Console.WriteLine("Value = {0}", value); }
7.通过Remove方法移除指定的键值
myDictionary.Remove(1); if(myDictionary.ContainsKey(1)) ...{ Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]); } else { Console.WriteLine("不存在 Key : 1"); }
5、其它常见属性和方法的说明:
Comparer: 获取用于确定字典中的键是否相等的 IEqualityComparer。
Count: 获取包含在 Dictionary中的键/值对的数目。
Item: 获取或设置与指定的键相关联的值。
Keys: 获取包含 Dictionary中的键的集合。
Values: 获取包含 Dictionary中的值的集合。
Add: 将指定的键和值添加到字典中。
Clear: 从 Dictionary中移除所有的键和值。
ContainsKey: 确定 Dictionary是否包含指定的键。
ContainsValue: 确定 Dictionary是否包含特定值。
GetEnumerator: 返回循环访问 Dictionary的枚举数。
GetType:获取当前实例的 Type。 (从 Object 继承。)
Remove:从 Dictionary中移除所指定的键的值。
ToString:返回表示当前 Object的 String。 (从 Object 继承。)
TryGetValue: 获取与指定的键相关联的值。
使用for循环遍历键值
Dictionary<string, int> Dict = new Dictionary<string, int>(); Dict .Add( "a", 1 ); Dict .Add( "b", 2 ); Dict .Add( "c", 3 ); Dict .Add( "d", 4 ); Dict .Add( "e", 5 ); Dict .Add( "f", 6 ); for(int i = 0 ; i < DicTest.Count ; i++) { Debug.Log(DicTest.ToList()[i].Key + ":" + DicTest.ToList()[i].Value); }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。如果你想了解更多相关内容请查看下面相关链接
本文共计1115个文字,预计阅读时间需要5分钟。
1. SortedDictionary泛型类+SortedDictionary泛型类是具有O(logn)搜索运算复杂度的二叉搜索树,其中n是字典中元素的数量。这一点与SortedList泛型类相似。这两个类都具有类似的对象模型。
1、SortedDictionary泛型类
SortedDictionary 泛型类是检索运算复杂度为 O(logn) 的二叉搜索树,其中n是字典中的元素数。就这一点而言,它与SortedList泛型类相似。这两个类具有相似的对象模型,并且都具有 O(logn) 的检索运算复杂度。这两个类的区别在于内存的使用以及插入和移除元素的速度:
- SortedList使用的内存比SortedDictionary少。
- SortedDictionary可对未排序的数据执行更快的插入和移除操作:它的时间复杂度为 O(logn),而SortedList为 O(n)。
- 如果使用排序数据一次性填充列表,则SortedList比SortedDictionary快。
每个键/值对都可以作为KeyValuePair结构进行检索,或作为DictionaryEntry通过非泛型IDictionary接口进行检索。
只要键用作SortedDictionary中的键,它们就必须是不可变的。SortedDictionary中的每个键必须是唯一的。键不能为空引用(在 Visual Basic 中为 Nothing),但是如果值类型TValue为引用类型,该值则可以为空。
SortedDictionary需要比较器实现来执行键比较。可以使用一个接受comparer参数的构造函数来指定IComparer泛型接口的实现;如果不指定实现,则使用默认的泛型比较器Comparer.Default。如果类型TKey实现System.IComparable泛型接口,则默认比较器使用该实现。
C# 语言的foreach语句,需要集合中每个元素的类型。由于SortedDictionary的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是KeyValuePair类型
2、要使用Dictionary集合,需要导入C#泛型命名空间
System.Collections.Generic(程序集:mscorlib)
3、Dictionary的描述
- 从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成
- 任何键都必须是唯一的
- 键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值
- Key和Value可以是任何类型(string,int,custom class 等)
4、Dictionary常用用法:以 key 的类型为 int , value的类型为string 为例
1.创建及初始化
Dictionary<int,string>myDictionary=newDictionary<int,string>();
2.添加元素
myDictionary.Add(1,"C#");
3.通过Key查找元素
if(myDictionary.ContainsKey(1)) { Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]); }
4.通过KeyValuePair遍历元素
foreach(KeyValuePair<int,string>kvp in myDictionary) ...{ Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value); }
5.仅遍历键 Keys 属性
Dictionary<int,string>.KeyCollection keyCol=myDictionary.Keys; foreach(intkeyinkeyCol) ...{ Console.WriteLine("Key = {0}", key); }
6.仅遍历值 Valus属性
Dictionary<int,string>.ValueCollection valueCol=myDictionary.Values; foreach(stringvalueinvalueCol) ...{ Console.WriteLine("Value = {0}", value); }
7.通过Remove方法移除指定的键值
myDictionary.Remove(1); if(myDictionary.ContainsKey(1)) ...{ Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]); } else { Console.WriteLine("不存在 Key : 1"); }
5、其它常见属性和方法的说明:
Comparer: 获取用于确定字典中的键是否相等的 IEqualityComparer。
Count: 获取包含在 Dictionary中的键/值对的数目。
Item: 获取或设置与指定的键相关联的值。
Keys: 获取包含 Dictionary中的键的集合。
Values: 获取包含 Dictionary中的值的集合。
Add: 将指定的键和值添加到字典中。
Clear: 从 Dictionary中移除所有的键和值。
ContainsKey: 确定 Dictionary是否包含指定的键。
ContainsValue: 确定 Dictionary是否包含特定值。
GetEnumerator: 返回循环访问 Dictionary的枚举数。
GetType:获取当前实例的 Type。 (从 Object 继承。)
Remove:从 Dictionary中移除所指定的键的值。
ToString:返回表示当前 Object的 String。 (从 Object 继承。)
TryGetValue: 获取与指定的键相关联的值。
使用for循环遍历键值
Dictionary<string, int> Dict = new Dictionary<string, int>(); Dict .Add( "a", 1 ); Dict .Add( "b", 2 ); Dict .Add( "c", 3 ); Dict .Add( "d", 4 ); Dict .Add( "e", 5 ); Dict .Add( "f", 6 ); for(int i = 0 ; i < DicTest.Count ; i++) { Debug.Log(DicTest.ToList()[i].Key + ":" + DicTest.ToList()[i].Value); }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。如果你想了解更多相关内容请查看下面相关链接

