Hashtable 的煩惱!

來源:互聯網
上載者:User
不知道把這麼一篇不入流的東西放在首頁有礙觀瞻,但就算是新手也一般是瀏覽首頁,我的目的也是希望給一些碰到類似問題的新手提供點協助,也希望得到高手的指點。
先看下面的代碼

using System;
using System.Collections;

namespace NoSortHashtable
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Hashtable hashTable = new Hashtable();

            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]);
            }

        }
    }
}

列印的結果是:
    anhui : hefei
    hunan : changsha
    sichuan : chengdu
    beijing : beijing
當然,產生這個結果的原因大家都知道,Hashtable內部的排序機制使然,但我現在就是不想排序,我按什麼順序輸入的,就想它再怎麼給我輸出,怎麼辦?去Google酷了一下,卻因為不知道使用什麼關鍵字去酷,結果沒有酷出好的相關問題來。
我想到,ArrayList是不排序的啊,那是不是讓ArrayList和Hastable配成良緣,那麼它們的結晶就是我想要的呢,既有Hashtable的豐富功能,又可以滿足我的BT的要求(不排序),動手了。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]);
            }

列印結果:
    hunan : changsha
    beijing : beijing
    anhui : hefei
    sichuan : chengdu

問題解決!!
應該很早之前就有人這麼解決,只是我不知道而已,高手也應該有更好的辦法,只是我想不到而已!
見笑,見諒!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.