问个Hashtable的问题,很多网上资料说Hashtable是不能根据Key值自动排序的,但是我在.netFramework2.0下测试的结果却是,遍历输出的时候却是根据Key值排序输出的,难道是可以根据键值排序存储的?-----------------------------------------------
Code Example:string myString = "1,AAA;2,BBB;3,CCC";
string[] myStringArray = myString.Split(';');
string[] mySubStringArray;
Hashtable ht = new Hashtable();
foreach(string str in myStringArray)
{
mySubStringArray = str.Split(',');
ht.Add(mySubStringArray[0], mySubStringArray[1]);
}
//遍历输出,发现是按照键值ASC顺序输出的
Console.WriteLine("Traverse Method 1:");
foreach(DictionaryEntry de in ht)
{
Console.Write("Key: "+de.Key);
Console.WriteLine(";Value: "+de.Value);
}
Console.WriteLine("\nTraverse Method 2:");
System.Collections.IDictionaryEnumerator enumerator=ht.GetEnumerator();
while(enumerator.MoveNext())
{
Console.Write("Key: "+enumerator.Key);
Console.WriteLine(";Value: "+enumerator.Value);
}
Code Example:string myString = "1,AAA;2,BBB;3,CCC";
string[] myStringArray = myString.Split(';');
string[] mySubStringArray;
Hashtable ht = new Hashtable();
foreach(string str in myStringArray)
{
mySubStringArray = str.Split(',');
ht.Add(mySubStringArray[0], mySubStringArray[1]);
}
//遍历输出,发现是按照键值ASC顺序输出的
Console.WriteLine("Traverse Method 1:");
foreach(DictionaryEntry de in ht)
{
Console.Write("Key: "+de.Key);
Console.WriteLine(";Value: "+de.Value);
}
Console.WriteLine("\nTraverse Method 2:");
System.Collections.IDictionaryEnumerator enumerator=ht.GetEnumerator();
while(enumerator.MoveNext())
{
Console.Write("Key: "+enumerator.Key);
Console.WriteLine(";Value: "+enumerator.Value);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货