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.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for NoSortedHashtable.
ExpandedSubBlockEnd.gif    
/// </summary>

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

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

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

ExpandedSubBlockEnd.gif        }

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

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

InBlock.gif        
public override IDictionaryEnumerator GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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


问题解决!!
应该很早之前就有人这么解决,只是我不知道而已,高手也应该有更好的办法,只是我想不到而已!
见笑,见谅!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值