zoukankan      html  css  js  c++  java
  • Hashtable和Dictionary<T,K>的使用

    由于Hashtable内部自带有排序(根据Key的HashCode来进行的),因此有时在使用Hashtable时就会造成数据顺序不可控的情况,有两种办法可以解决,

    测试代码:

     Dictionary<string,string> ht=new Dictionary<string, string>();
            ht.Add("http://www.sina.com.cn","");
            ht.Add("http://www.bjut.edu.cn","");
            ht.Add("http://lib.bjut.edu.cn", "");
            ht.Add("http://news.bjut.edu.cn", "");
            ht.Add("http://sse.bjut.edu.cn", "");
            ht.Add("http://lexus.cnblogs.com", "");
            ht.Add("http://www.sina.com.cn/sport", "");
            ht.Add("http://www.sina.com.cn/ent", "");

            foreach(var kvp in ht)
                Console.WriteLine(kvp.Key);
            Console.WriteLine("============================================");
            Hashtable ht2=new Hashtable();
            ht2.Add("http://www.sina.com.cn", "");
            ht2.Add("http://www.bjut.edu.cn", "");
            ht2.Add("http://lib.bjut.edu.cn", "");
            ht2.Add("http://news.bjut.edu.cn", "");
            ht2.Add("http://sse.bjut.edu.cn", "");
            ht2.Add("http://lexus.cnblogs.com", "");
            ht2.Add("http://www.sina.com.cn/sport", "");
            ht2.Add("http://www.sina.com.cn/ent", "");
            foreach(DictionaryEntry i in ht2)
                Console.WriteLine(i.Key);

    第一种是继承Hashtable,自己创建一个新的类,用一个ArrayList对象保存keys;

    代码:(转)

    using System;
    using System.Collections;

    namespace NoSortHashtable
    {
        /// <summary>
        /// Summary description for NoSortedHashtable.
        /// </summary>
        public class NoSortHashtable : Hashtable
        {
            private ArrayList keys = new ArrayList();

            public NoSortHashtable()
            {
            }
            

            public override void Add(object key, object value)
            {
                base.Add (key, value);
                keys.Add (key);
            }

            public override ICollection Keys
            {
                get
                {
                    return keys;
                }
            }

            public override void Clear()
            {
                base.Clear ();
                keys.Clear ();
            }

            public override void Remove(object key)
            {
                base.Remove (key);
                keys.Remove    (key);
            }
            public override IDictionaryEnumerator GetEnumerator()
            {
                return base.GetEnumerator ();
            }

        }
    }

    测试:

                hashTable = new NoSortHashtable();

                hashTable.Add("hunan","changsha");
                hashTable.Add("beijing","beijing");
                hashTable.Add("anhui","hefei");
                hashTable.Add("sichuan","chengdu");
                foreach(string str in hashTable.Keys)
                {
                    Console.WriteLine(str + " : " + hashTable[str]);
                }

    ----------------------------------------------------------------------

    第二种办法是采用泛型的Dictionary<T,K>对象,该对象按照插入的顺序输出;

             Dictionary<string,string> ht=new Dictionary<string, string>();
            ht.Add("http://www.sina.com.cn","");
            ht.Add("http://www.bjut.edu.cn","");
            ht.Add("http://lib.bjut.edu.cn", "");
            ht.Add("http://news.bjut.edu.cn", "");
            ht.Add("http://sse.bjut.edu.cn", "");
            ht.Add("http://lexus.cnblogs.com", "");
            ht.Add("http://www.sina.com.cn/sport", "");
            ht.Add("http://www.sina.com.cn/ent", "");

              foreach(var kvp in ht)
                  Console.WriteLine(kvp.Key);

  • 相关阅读:
    Ajax调用处理页面错误信息500的解决思路
    PHP数据库的增删改
    PHP登录及处理
    PHP数据访问
    PHP数组(正则表达式、数组、预定义数组)
    php函数
    45
    数据库_CRUD操作之读取数据之高级读取
    数据库_CRUD操作之读取数据
    数据库_CRUD操作之修改数据
  • 原文地址:https://www.cnblogs.com/coolsundy/p/4202554.html
Copyright © 2011-2022 走看看