九.构建字典:DictionaryBase类和SortedList类
字典是一种把数据作为键/值对来存储的数据结构。可以通过自定义一个继承自System.Collections.DictionaryBase的类,来建立适合的字典。System.Collections.Generic . SortedList类可以通过引用索引号的方式来反问值和键,使用方式和数组很相似。
DictionaryBase类(基于键来存储键值对)
DictionaryBase类
为键/值对的强类型集合提供abstract 基类。
内部,会把键值对存储在被称为InnerHashtable的散列表对象中。
无法单独操作修改某个键或者值。
public abstractclass DictionaryBase:IDictionary, ICollection,IEnumerable
例子:
public class Sqrt:System.Collections.DictionaryBase
{
public Sqrt()
{ }
public void Add(int value, intresult)
{ base.InnerHashtable.Add(value, result); }
public string Item(int value)
{ return base.InnerHashtable[value].ToString();}
public void Remove(int value)
{ base.InnerHashtable.Remove(value); }
public void CopyTo(Array array, intindex)
{ base.InnerHashtable.CopyTo(array, index); }
}
储存的键值对,实际上是作为DictionaryEntry对象存储的。不能依据位置索引号来访问。
Add方法
添加新数据。
Item方法
取回数据。
Remove方法
移除掉键值对。
Clear方法
清除所有数据的数据结构。
CopyTo方法
把字典的内容复制给一个数组,两个参数:要复制到的数组和开始复制的索引位置。
Sqrt sqrt = newSqrt();
sqrt.Add(0, 0);
sqrt.Add(1, 1);
sqrt.Add(2, 4);
sqrt.Add(3, 9);
sqrt.Add(4, 16);
sqrt.Add(5, 25);
sqrt.Remove(5);
Console.WriteLine(sqrt.Item(3));
///9
System.Collections.DictionaryEntry[]ar =new System.Collections.DictionaryEntry[sqrt.Count ];
sqrt.CopyTo(ar,0);
DictionaryEntry类
// 摘要:
// 定义可设置或检索的字典键/值对。
[Serializable]
[ComVisible(true)]
public struct DictionaryEntry
{
//
// 摘要:
// 使用指定的键和值初始化 System.Collections.DictionaryEntry类型的实例。
//
// 参数:
// key:
// 每个键/值对中定义的对象。
//
// value:
// 与 key相关联的定义。
//
// 异常:
// System.ArgumentNullException:
// key 为 null,并且 .NET Framework版本为 1.0 或 1.1。
publicDictionaryEntry(object key,object value);
// 摘要:
// 获取或设置键/值对中的键。
//
// 返回结果:
// 键/值对中的键。
public object Key { get; set; }
//
// 摘要:
// 获取或设置键/值对中的值。
//
// 返回结果:
// 键/值对中的值。
public object Value { get; set; }
}
DictionaryEntry对象内的数据,可以根据数组的索引号来查看键和值。
System.Collections.DictionaryEntry[]ar =new System.Collections.DictionaryEntry[sqrt.Count ];
sqrt.CopyTo(ar,0);
for (inti = 0; i < ar.GetUpperBound(0); i++)
{
Console.Write("{0},{1};", ar[i].Key, ar[i].Value);
}
///4,16;3,9;2,4;1,1;
泛型KeyValuePair类(基于键来存储键值)
// 摘要:
// 定义可设置或检索的键/值对。
//
// 类型参数:
// TKey:
// 键的类型。
//
// TValue:
// 值的类型。
[Serializable]
public struct KeyValuePair<TKey,TValue>
{
//
// 摘要:
// 用指定的键和值初始化System.Collections.Generic.KeyValuePair<TKey,TValue>结构的新实例。
//
// 参数:
// key:
// 每个键/值对中定义的对象。
//
// value:
// 与 key相关联的定义。
publicKeyValuePair(TKey key, TValue value);
// 摘要:
// 获取键/值对中的键。
//
// 返回结果:
// 一个 TKey,它是System.Collections.Generic.KeyValuePair<TKey,TValue>的键。
publicTKey Key { get; }
//
// 摘要:
// 获取键/值对中的值。
//
// 返回结果:
// 一个 TValue,它是 System.Collections.Generic.KeyValuePair<TKey,TValue>的值。
publicTValue Value { get; }
// 摘要:
// 使用键和值的字符串表示形式返回System.Collections.Generic.KeyValuePair<TKey,TValue>的字符串表示形式。
//
// 返回结果:
// System.Collections.Generic.KeyValuePair<TKey,TValue>的字符串表示形式,它包括键和值的字符串表示形式。
public override stringToString();
}
基于键来存储数据,每个对象只能保存一个键和一个值。
KeyValuePair<string,int>mcmillan = newKeyValuePair<string,int>("McMillan", 99);
Console.WriteLine("{0},{1};", mcmillan.Key,mcmillan.Value);
//McMillan,99;
SortedList类(按照键的顺序来存储键值对)
System.Collections.Generic.SortedList按照分类顺序,基于键值来存储键值对,是DictionaryBase类的特殊化,没有继承自DictionaryBase。可通过指定索引号对键集合、值集合进行访问。
无法单独操作修改键,但能单独修改某个键的值。
public classSortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey,TValue>>,IEnumerable<KeyValuePair<TKey, TValue>>,IDictionary, ICollection,IEnumerable
构造
public SortedList();
public SortedList(IComparer<TKey>comparer);
public SortedList(IDictionary<TKey,TValue> dictionary);
public SortedList(intcapacity);
public SortedList(IDictionary<TKey,TValue> dictionary,IComparer<TKey>comparer);
public SortedList(intcapacity,IComparer<TKey> comparer);
Add方法
添加新数据。
Remove方法
移除掉指定键的键值对。
RemoveAt方法
移除掉指定键索引号的键值对。
Clear方法
清除所有数据的数据结构。
IndexOfKey方法
获取指定键在键集合中的索引号。
IndexOfValue方法
获取指定值在值集合中的索引号。
ToArray方法
将键值对数据创建到一个KeyValuePair<T,T>数组中。
KeyValuePair<string,string>[] arrr= myips.ToArray();
Keys属性
返回键的集合。通过索引号,可以查询指定位置上的键数据。
Values属性
返回值的集合。通过索引号,可以查询指定位置上的值数据。
SortedList<string,string> myips = newSortedList<string,string>();
myips.Add("Mike", "192.155.12.1");
myips.Add("Davic", "192.155.12.2");
myips.Add("Bernica","192.155.12.3");
for (inti = 0; i < myips.Count; i++)
{
Console.WriteLine("[{0}]:{1},{2}[{3}];",myips.IndexOfKey(myips.Keys[i]), myips.Keys[i], myips.Values[i],myips.IndexOfValue(myips.Values[i]));
}
///[0]:Bernica,192.155.12.3[0];
///[1]:Davic,192.155.12.2[1];
///[2]:Mike,192.155.12.1[2];