Hashtable,挺爽的一个东西,大家都用烂了吧,我再画蛇添足一下。

以前分析数据的时候,用过这个东西,当时连叫了几声“爽”,今天刚好有又朋友问起这个,补充几点吧。
System.Collections.Hashtable是用来表示一组组key/value结构的容器,可能别的语言会把它叫做Map,Dictionary的,其结构中Key用来快速查找,所以,叫它Dictionary可能更加合适。
Hashtable的方法、属性并不多,大多数也可以望文生正义,只是有一点,Hashtable提供了hashTable[aKey]的方式引用其包含的对象,却并没有提供数字指向的索引器,就是说用惯了Array,数组的我们,不能用hashTable[0]之类的办法来检索它的内容,继而,我们也就不能用
for(int i =0 ; i < hashTable.count ; i ++)
{
 Console.WriteLine("Key -- {0}; Value --{1}.",hashTable[i].key,hashTable[i].value);
}
这样的代码块来遍历,而我们把一个个的键值对加入HashTable中,很多情况下都是最终要把它遍历出来,那么怎么做呢?以下代码将解答这个问题。
foreach( DictionaryEntry de in hashTable)
{
 console.WriteLine("Key -- {0}; Value --{1}.", de.Key, de.Value);
}

不过还是习惯用index来访问的话,System.Collections.Specialized.NameValueCollection是一个可以用index访问的类,不爽的是它的Key/Value都必须是string数据类型。
记得System.Collections.Specialized.ListDictionary是一个单向链表的结构,所以如果Key/Value对不多的话,它的效率该比Hashtable高。
System.Collections.Specialized.StringDictionary的话Key是区分大小写的string数据类型。


None.gif             Hashtable hash  =   new  Hashtable();
None.gif            
for ( int  i  =   0  ; i  <  id.Length ; i  ++ )
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
if(id[i].Trim() != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string builId = id[i];
InBlock.gif                    
string buildept = G.Getxxxx(builId).Rows[0]["xxxx"].ToString().Trim();
InBlock.gif                    
if(!hash.Contains(buildept))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        hash.Add(buildept,builId);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        hash[buildept] 
= (string)hash[buildept] + "," + builId;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

None.gif            
foreach (DictionaryEntry de  in  hash)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
string dept = de.Key.ToString().Trim();
InBlock.gif                
string ids = de.Value.ToString().Trim();
ExpandedBlockEnd.gif            }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include #include using namespace std; #define NULL 0 unsigned int key; //用来输入/输出文件流类 unsigned int key2; //key和key2分别是用做了电话号码和姓名的关键字 int *p; struct node //新建节点(用户姓名、地址、电话号码、指向下一个结点的指针 ) { char name[8],address[20]; char num[11]; node * next; }; typedef node* pnode; typedef node* mingzi; //声明了名字和电话两个指针 node **phone; node **nam; node *a; void hash(char num[11]) //以电话号码为关键字建立哈希函数 { int i = 3; key=(int)num[2]; while(num[i]!=NULL) { key+=(int)num[i]; i++; } key=key%20; } void hash2(char name[8]) //姓名为关键字建立哈希函数 { int i = 1; key2=(int)name[0]; while(name[i]!=NULL) { key2+=(int)name[i]; i++; } key2=key2%20; } //强制类型转换,将用户名的每一个字母的ASCLL码值相加并且除以20后的余数 node* input() //输入节点信息 ,建立结点,并将结点的next指针指空 { node *temp; temp = new node; temp->next=NULL; cout<<"输入姓名:"<>temp->name; cout<<"输入地址:"<>temp->address; cout<<"输入电话:"<>temp->num; return temp; } //对于指针类型返回的是地址 int apend() //添加节点 { node *newphone; node *newname; newphone=input(); newname=newphone; newphone->next=NULL; newname->next=NULL; hash(newphone->num); //利用哈希函数计算出对应关键字的存储地址 hash2(newname->name); newphone->next = phone[key]->next; //利用电话号码为关键字插入 phone[key]->next=newphone; //是采用链地址法,拉链法处理冲突的散列表结构 newname->next = nam[key2]->next; //利用用户名为关键字插入 nam[key2]->next=newname; return 0; } void create() //新建节点 { int i; phone=new pnode[20]; //动态创建对象数组,C++课本P188页 for(i=0;inext=NULL; } } void create2() //新建节点 { int i; nam=new mingzi[20]; for(i=0;inext=NULL; } } void list() //显示列表 { int i; node *p; for(i=0;inext; while(p) { cout<name<<'_'<address<<'_'<num<next; } } } void list2() //显示列表 { int i; node *p; for(i=0;inext; while(p) { cout<name<<'_'<address<<'_'<num<next; } } } void find(char num[11]) //在以电话号码为关键字的哈希表中查找用户信息 { hash(num); node *q=phone[key]->next; while(q!= NULL) { if(strcmp(num,q->num)==0) break; q=q->next; } if(q) cout<name<<"_" <address<<"_"<num<<endl; else cout<<"无此记录"<next; while(q!= NULL) { if(strcmp(name,q->name)==0) break; q=q->next; } if(q) cout<name<<"_" <address<<"_"<num<<endl; else cout<<"无此记录"<<endl; } void save() //保存用户信息 { int i; node *p; for(i=0;inext; while(p) { fstream iiout("out.txt", ios::out); //创建一个文件流对象:iiout iiout<name<<"_"<address<<"_"<num<next; } } } void menu() //菜单 { cout<<" 哈希表通讯录"<<endl; cout<<" 0.添加记录"<<endl; cout<<" 2.姓名散列"<<endl; cout<<" 3.查找记录"<<endl; cout<<" 4.号码散列"<<endl; cout<<" 5.清空记录"<<endl; cout<<" 6.保存记录"<<endl; cout<<" 7.退出系统"<>sel; if(sel==3) { cout<<"8姓名查询" <<endl;cout<<"9号码查询"<>b; if(b==9) {cout<<"请输入电话号码:"<>num; cout<<"输出查找的信息:"<<endl; find(num); } else {cout<<"请输入姓名:"<>name; cout<<"输出查找的信息:"<<endl; find2(name);}} if(sel==2) {cout<<"姓名散列结果:"<<endl; list2();} if(sel==0) {cout<<"请输入要添加的内容:"<<endl; apend();} if(sel==4) {cout<<"号码散列结果:"<<endl; list(); } if(sel==5) {cout<<"列表已清空:"<<endl; create();create2();} if(sel==6) { cout<<"通信录已保存:"<<endl; save();} if(sel==7) return 0; } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值