主题:有关MSDN中hashtable中一个例子的疑问,望高手指点!
using System;
using System.Collections;
public class SamplesHashtable {
public static void Main() {
// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add( "one", "The" );
myHT.Add( "two", "quick" );
myHT.Add( "three", "brown" );
myHT.Add( "four", "fox" );
// Displays the Hashtable.
Console.WriteLine( "The Hashtable contains the following:" );
PrintKeysAndValues( myHT );
}
public static void PrintKeysAndValues( Hashtable myList ) {
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
Console.WriteLine( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.MoveNext() )
Console.WriteLine( "\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value );
Console.WriteLine();
}
}
/*
This code produces the following output.
The Hashtable contains the following:
-KEY- -VALUE-
three: brown
four: fox
two: quick
one: The
*/
这个例子中通过add方法添加了4个键值对,但为什么在输出的时候却没有按添加的顺序输出呢?难道是在获取枚举数的时候打乱了顺序?我又试着添加了多个值键对,但输出的结果更加混乱了,也找不出任何的规律,大虾们能帮我解答么?[em1]
using System.Collections;
public class SamplesHashtable {
public static void Main() {
// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add( "one", "The" );
myHT.Add( "two", "quick" );
myHT.Add( "three", "brown" );
myHT.Add( "four", "fox" );
// Displays the Hashtable.
Console.WriteLine( "The Hashtable contains the following:" );
PrintKeysAndValues( myHT );
}
public static void PrintKeysAndValues( Hashtable myList ) {
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
Console.WriteLine( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.MoveNext() )
Console.WriteLine( "\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value );
Console.WriteLine();
}
}
/*
This code produces the following output.
The Hashtable contains the following:
-KEY- -VALUE-
three: brown
four: fox
two: quick
one: The
*/
这个例子中通过add方法添加了4个键值对,但为什么在输出的时候却没有按添加的顺序输出呢?难道是在获取枚举数的时候打乱了顺序?我又试着添加了多个值键对,但输出的结果更加混乱了,也找不出任何的规律,大虾们能帮我解答么?[em1]