/*
我们查询资料得知Dictionary的遍历顺序和添加Add时的顺序是一致的,不像 HashTable 顺序不可知;于是我要依赖Dictionary的这种顺序一致特性做一个,固定大小400长度的队列,如果超过400个元素就Remove删除掉老的数 据,保留新的数据;我为什么不用队列Queue因为它没有ContainsKey键查找功能,因为我需要用键来去重.
      
当我的程序运行一段时间后发生逻辑问题
,本来我的程序逻辑是超是400后删除Remove最添加的老数据,但是删除的却是最后添加的新数据,当遍历的时候顺序打乱了,并不是添加的程序,经测试发现字典对象并不能一定保持程序一致,一个使用Remove顺序一致性将得不到保证,经测试
*/ SortedDictionary<string, string> testdict = new SortedDictionary<string, string>();
testdict.Add("a", "a");
testdict.Add("b", "b");
testdict.Add("c", "c");
testdict.Remove("a");
testdict.Add("a", "a");
foreach (KeyValuePair<string, string> kv in testdict)
{
Response.Write(kv.Key);//输出abc 而不是 bca
} Dictionary<string, string> testdict = new Dictionary<string, string>();
testdict.Add("a", "a");
testdict.Add("b", "b");
testdict.Add("c", "c");
testdict.Remove("a");
testdict.Add("a", "a");
foreach (KeyValuePair<string, string> kv in testdict)
{
Response.Write(kv.Key);//输出abc 而不是 bca
}
一但使用了Remove字典对象的程序,它已经乱了,现在只有依赖字典加对象双结合才能达到我的要求,悲剧

警惕Dictionary和SortedDictionary的顺序陷阱的更多相关文章

  1. 深入解析Hashtable、Dictionary、SortedDictionary、SortedList

    我们先看Hashtable. MSDN的解释:表示键/值对的集合,这些键/值对根据键的哈希代码进行组织. Hash算法是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定 ...

  2. 常用数据结构及复杂度 array、LinkedList、List、Stack、Queue、Dictionary、SortedDictionary、HashSet、SortedSet

    原文地址:http://www.cnblogs.com/gaochundong/p/data_structures_and_asymptotic_analysis.html  常用数据结构的时间复杂度 ...

  3. Dictionary、SortedDictionary、Hashtable 、SortedList

    HashTable数据结构存在问题:空间利用率偏低.受填充因子影响大.扩容时所有的数据需要重新进行散列计算.虽然Hash具有O(1)的数据 检索效率,但它空间开销却通常很大,是以空间换取时间.所以Ha ...

  4. Dictionary与SortedDictionary

    Dictionary是无序的,如果想排序,需要使用SortDictionary. 下面是一个用法示例 //按照某个字段排序 public void SortByCardItem(string item ...

  5. [css]需警惕CSS3属性的书写顺序

    转载张鑫旭:http://www.zhangxinxu.com/wordpress/2010/09/%E9%9C%80%E8%AD%A6%E6%83%95css3%E5%B1%9E%E6%80%A7% ...

  6. 警惕Java编译器中那些“蜜糖”陷阱

    一.前言 随着Java编译器不断地向前发展,它为程序员们提供了越来越多的“蜜糖”(compiler suger),极大地方便了程序的开发,例如,foreach的增强模式,自动拆箱与装箱以及字符串的连接 ...

  7. C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)

    我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类.我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和 ...

  8. 泛型Dictionary的用法详解

    泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的 ...

  9. 类 Array Arraylist List Hashtable Dictionary

    总结C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue  我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashT ...

随机推荐

  1. Linux/hp unix/AIX日常巡检脚本(转)

    以下为Linux/hp unix/AIX日常巡检脚本,大家可以参考着进行改写,用于自己的服务器. #!/usr/bin/ksh syserrdate=`date +"%m/%d"` ...

  2. lpc1768usb端点响应以及描述符定义

    #ifndef __USBEP1_H_ #define __USBEP1_H_ #include "usb.h" #include "usbhw.h" exte ...

  3. bzoj1176: [Balkan2007]Mokia【cdq分治】

    把询问搞成4个,cdq分治. #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a;i <= b; i++) #d ...

  4. jQuery简单实现图片预加载

    我们在做网站的时候经常会遇到这样的问题:一个页面有大量的图片导致页面加载速度缓慢,经常会出现一个白页用户体验很不好.那么如何解决这个问题呢?下面我来介绍一种在实际应用中经常会使用到的js预加载的方法. ...

  5. coding菜鸟养成记

    http://www.cnblogs.com/xdp-gacl/category/563690.html http://www.cnblogs.com/vincent-blog/p/4402327.h ...

  6. Oracle 事件

    Oracle 的事物 事物是设么 事物是用于高正数据的一致性,他由一组相关的dml语句组成(增加删除语句),这组语句要么全部成功要不全部失败. 如:网上转账. 1)设置保存点 Savepoint a1 ...

  7. xml的生成和发送

    s2014-04-07 10:01:05 之前学的是解析,现在需要生成xml, 然后利用蓝牙或者wifi发送到服务器 2014-04-07 10:36:34 采用dom4j创建xml报错 后来发现安卓 ...

  8. 使用Python脚本操作MongoDB的教程

    Reference:  http://www.jb51.net/article/64225.htm

  9. [HNOI2004]Language L语言

    2777: [HNOI2004]Language L语言 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 10  Solved: 5[Submit][S ...

  10. React Native 之 HelloWorld

    1. 切换目录 输入之前要切换到要保存的目录 2. 修改下载源 cd ~/ vim .npmrc 添加 registry = https://registry.npm.taobao.org 3. 在终 ...