首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

为啥结果会是从后面的那个读起,不是从第一个开始读起

2012-10-14 
为什么结果会是从后面的那个读起,不是从第一个开始读起?/// summary/// 使用hashtable显示键值对/// /s

为什么结果会是从后面的那个读起,不是从第一个开始读起?
/// <summary>
  /// 使用hashtable显示键值对
  /// </summary>
  public static void UseNonGenericHashtable()
  {
  Hashtable numbers = new Hashtable();
  numbers.Add(1, "one");
  numbers.Add(2, "two");

  foreach (DictionaryEntry de in numbers)
  {
  Console.WriteLine("Key:" + de.Key + "\tvalue:" + de.Value);
  }
  numbers.Clear();
  }
 运行结果为 Key:2 value:two
  Key:1 value:one

[解决办法]
Hashtable ,这种的存储键值对的,就是没有顺序
[解决办法]

C# code
 Dictionary<int, string> dic = new Dictionary<int, string>();            dic.Add(1, "one");            dic.Add(2, "two");            foreach (var de in dic)            {                Console.WriteLine("Key:" + de.Key + "\tvalue:" + de.Value);            }            dic.Clear();
[解决办法]
键值对 我还是喜欢用 dictionary

探讨
C# code

Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "one");
dic.Add(2, "two");
foreach (var de in dic)
{
……

[解决办法]
哈希表就是散列表,没有一定的读取顺序的

热点排行