Hashtable 的烦恼!

不知道把这么一篇不入流的东西放在首页有碍观瞻,但就算是新手也一般是浏览首页,我的目的也是希望给一些碰到类似问题的新手提供点帮助,也希望得到高手的指点。
先看下面的代码

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的要求(不排序),动手了。
None.gif using  System;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  NoSortHashtable
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// Summary description for NoSortedHashtable.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class NoSortHashtable : Hashtable
ExpandedSubBlockStart.gif    
{
InBlock.gif        
private ArrayList keys = new ArrayList();
InBlock.gif
InBlock.gif        
public NoSortHashtable()
ExpandedSubBlockStart.gif        
{
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
InBlock.gif        
public override ICollection Keys
ExpandedSubBlockStart.gif        
{
InBlock.gif            
get
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return keys;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Clear()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
base.Clear ();
InBlock.gif            keys.Clear ();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Remove(object key)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
base.Remove (key);
InBlock.gif            keys.Remove    (key);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override IDictionaryEnumerator GetEnumerator()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
return base.GetEnumerator ();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

再试
            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

转载于:https://www.cnblogs.com/zhangchenliang/archive/2011/04/22/2025129.html

猜你喜欢

转载自blog.csdn.net/weixin_33849942/article/details/93496072