哈希表(C#)中的多个值 [英] multi values in Hashtable (c#)

查看:112
本文介绍了哈希表(C#)中的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建具有三个参数的hashTable?我想使用hashTable存储电话号码,姓名和地址.电话号码为键,名称,地址为值.但是我只能输入两个数据,电话号码和姓名.如何在哈希表中保存电话号码,姓名,地址?

How can I make a hashTable with three parameters? I want to store phone numbers, names and addresses using a hashTable. Phone number as the key, and the name, address as its value. But I can put two data only, phone number and name. How do I get to save a phone number, name, address in the hashTable?

Hashtable phoneBook;

public FrmPhoneBook()
{
    InitializeComponent();
    phoneBook = new Hashtable();
}

public void addNewPhoneBook(string name, string tel, string add)
{
    string names = name;
    string telp = tel;
    string address = add;

    if (!phoneBook.ContainsKey(telp))
    {
        phoneBook.Add(telp, names);
        getDetails();
    }
}

public void getDetails()
{
    lvDetails.Items.Clear();
    foreach (DictionaryEntry values in phoneBook)
    {
        lvDetails.Items.Add(values.Value.ToString());
        lvDetails.Items[lvDetails.Items.Count - 1].SubItems.Add(
           values.Key.ToString());  
    }
}

推荐答案

首先,使用< codey> System.Collections.Generic:<代码>字典(我想说这是候选#1),SortedDictionarySortedList.所有这些类型都用关键字编制索引,并提供计算复杂度O 1.用于按索引搜索;区别主要是速度和内存开销之间的权衡.通过在.NET 2.0中引入泛型,使非泛型类型成为过时的(对于新开发;正式而言,它们不是过时的).非通用容器要求的类型强制转换是一件很糟糕的事情.

现在,这个问题的答案非常简单:创建一个具有地址和名称成员的结构或类;像以前一样将此类用作值类型,并将电话用作键类型.

-SA
First of all, instead of Hashtable, use one of generic classes from <codey>System.Collections.Generic: <code>Dictionary (I would say, this is a candidate #1), SortedDictionary or SortedList. All these types are indexed with the key and provide computational complexity O<1> for the search by index; the difference is mostly the trade-off between speed and memory overhead. Non-generic type are rendered obsolete (for new development; formally, they are not obsolete) by introduction of generics in .NET 2.0. Type cast required by non-generic containers is a really bad thing.

Now, the answer to the question is really simple: create a structure or a class with the members for address and name; use this class as a value type, and the telephone as the key type as you did before.

—SA


您可以使用字符串列表而不是单个字符串值.尝试ff:

you can use a List of string instead of a single string value. Try the ff:

List<string> details = new List<string>();
list.Add(tel);
list.Add(add); 
</string></string>



然后使用nametel作为键



and then use name or tel as key

HashTable obj = new HashTable();
obj.Add(name, details);
//or obj.Add(tel, details);



如果这解决了您的问题,请标记为答案并投票5

问候,
爱德华(Eduard)



Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard



这就是我要做的:
创建一个新类:Contact,具有三个属性:Phone,Name,Address,其余代码类似于:

This is what I would do:
Create a new class: Contact, with three properties: Phone, Name, Address, and the rest of the code would be something like :

public void addNewPhoneBook(string name, string tel, string add)
{
    var contact = new Contact(name, tel, add); 
    if (!phoneBook.ContainsKey(tel))
    {
        phoneBook.Add(tel, contact);
        getDetails();
    }
}


用于getDetails ...


for getDetails ...

public void getDetails()
{
    lvDetails.Items.Clear();
    lvDetails.Items = phoneBook.Values;
}


我没有编译此代码,但我认为它必须可以工作.
最好有一个Dictionary而不是Hashtable,您的字典可以定义为


I did not compile this code but I think it must work.
It is better to have a Dictionary instead of a Hashtable, your dictionary could be defined as

Dictionary<string, phoneBook> = new Dictionary<string, phoneBook>();


这篇关于哈希表(C#)中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆