C# SortedList 类实现了 IDictionary 接口 ,集合中的值都是以键值对的形式存取的。
SortedList 排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。
如果使用索引访问各项,则它是一个 ArrayList,如果使用键访问各项,则它是一个 Hashtable。
SortedList 类提供的构造方法有很多,最常用的是不含参数的构造方法。
SortedList 对象名 = new SortedList();
下面介绍 SortedList 类的方法和属性:
一、常用方法
1、SortedList.Add(Object, Object) 方法
将带有指定键和值的元素添加到 SortedList 对象。
public virtual void Add (object key, object value);
指定键 key 唯一,但 value可以为 null。
SortedList test = new SortedList
{
{001,"张三"},
{002,"李四"},
{003,null},
{004,"王五"}
};
foreach (DictionaryEnty v in test)
{
Console.WriteLine("键值{0},对应的数值是{1}",v.Key,v.Value);
}
2、SortedList.Clear 方法
从 SortedList 对象中移除所有元素。
public virtual void Clear ();
SortedList test = new SortedList
{
{001,"张三"},
{002,"李四"},
{003,null},
{004,"王五"}
};
foreach (DictionaryEnty v in test)
{
Console.WriteLine("键值{0},对应的数值是{1}",v.Key,v.Value);
}
test.Clear();
foreach (DictionaryEnty v in test)
{
Console.WriteLine("键值{0},对应的数值是{1}",v.Key,v.Value);
}
3、SortedList.Contains(Object) 方法
确定 SortedList 对象是否包含特定键。
public virtual bool Contains (object key);
如果 key 对象包含带有指定 SortedList 的元素,则为 true;否则为 false。
SortedList test = new SortedList
{
{1,234},
{2,345},
{3,456},
{4,567}
};
Console.WriteLine(test.Contains(3) ? "包含这个数" : "不包含这个数");
Contains 可实现 IDictionary.Contains。 它的行为与 ContainsKey完全相同。
4、SortedList.ContainsKey(Object) 方法
确定 SortedList 对象是否包含特定键。
public virtual bool ContainsKey (object key);
如果 key 对象包含带有指定 SortedList 的元素,则为 true;否则为 false。
SortedList test = new SortedList
{
{1,234},
{2,345},
{3,456},
{4,567}
};
Console.WriteLine(test.ContainsKey(5) ? "包含这个数" : "不包含这个数");
5、SortedList.ContainsValue(Object) 方法
确定 SortedList 对象是否包含特定值。
public virtual bool ContainsValue (object value);
如果 value 对象包含带有指定 SortedList 的元素,则为 true;否则为 false。
SortedList test = new SortedList
{
{1,234},
{2,345},
{3,456},
{4,567}
};
Console.WriteLine(test.ContainsValue(345) ? "包含这个数" : "不包含这个数");
6、SortedList.CopyTo(Array, Int32) 方法
从指定数组索引开始将 SortedList 元素赋值到一维 Array 对象中。
public virtual void CopyTo (Array array, int arrayIndex);
源 SortedList 对象中的元素数目要小于等于从 arrayIndex 到目标 array 末尾之间的可用空间。
SortedList test = new SortedList
{
{"ds","qaq"},
{"sd","bab"},
{"we","ckc"}
};
String[] demo= new String[] { "The", "quick", "brown", "fox", "jumps", "over"};
test.Values.CopyTo(demo,2);
test.Keys.CopyTo(demo,3);
foreach(var v in demo)
{
Console.WriteLine(v);
}
若要仅复制 SortedList中的密钥,请使用 SortedList.Keys.CopyTo。
若要仅复制 SortedList中的值,请使用 SortedList.Values.CopyTo。
7、SortedList.Remove(Object) 方法
从 SortedList 对象中移除带有指定键的元素。
public virtual void Remove (object key);
SortedList test = new SortedList
{
{1,"张三"},
{2,"李四"},
{3,"王五"},
{4,"马六"}
};
test.Remove(3);
foreach(DictionaryEntry de in test)
{
Console.WriteLine("键值{0}对应的数值是{1}",de.Key,de.Value);
}
8、SortedList.RemoveAt(Int32) 方法
移除 SortedList 对象的指定索引处的元素。
public virtual void RemoveAt (int index);
SortedList test = new SortedList
{
{1,"张三"},
{2,"李四"},
{3,"王五"},
{4,"马六"}
};
test.RemoveAt(3);
foreach(DictionaryEntry de in test)
{
Console.WriteLine("键值{0}对应的数值是{1}",de.Key,de.Value);
}
9、SortedList.TrimToSize 方法
将容量设置为 SortedList 对象中元素的实际数目。
public virtual void TrimToSize ();
SortedList test = new SortedList
{
{ "3c", "dog" },
{ "2c", "over" },
{ "1c", "brown" },
{ "1a", "The" },
{ "1b", "quick" },
{ "3a", "the" },
{ "3b", "lazy" },
{ "2a", "fox" },
{ "2b", "jumps" }
};
test.TrimToSize();
二、常用属性
1、SortedList.Capacity 属性
获取或设置 SortedList 对象可包含的容量。
public virtual int Capacity { get; set; }
SortedList test = new SortedList
{
{ "3c", "dog" },
{ "2c", "over" },
{ "1c", "brown" },
{ "1a", "The" },
{ "1b", "quick" },
{ "3a", "the" },
{ "3b", "lazy" },
{ "2a", "fox" },
{ "2b", "jumps" }
};
Console.WriteLine(test.Capacity.ToString());
2、SortedList.Count 属性
获取 SortedList 对象中的实际包含的元素数。
public virtual int Count { get; }
SortedList test = new SortedList
{
{ "3c", "dog" },
{ "2c", "over" },
{ "1c", "brown" },
{ "1a", "The" },
{ "1b", "quick" },
{ "3a", "the" },
{ "3b", "lazy" },
{ "2a", "fox" },
{ "2b", "jumps" }
};
Console.WriteLine(test.Count.ToString());