关于字典的问题
我按照PROFESSIONAL C#中的例子的思路写了一个字典的类,但是我用HASHTABLE对象怎么也无法查询,我不知道问题在哪里,下面是程序.(我在调试的时候发现HashTable对象中是有值的,而且我定义的查找键key也是能够读入用户输入信息的,但是ht[key]返回的却总是null,即使key输入存在的键值也返回null.究竟是哪里出了问题?)
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace DictionaryTest
{
class DataID
{
private string message;
public DataID(string message)
{
this.message = message;
}
public override string ToString()
{
return message;
}
public override int GetHashCode()
{
return message.GetHashCode();
}
public override bool Equals(object obj)
{
DataID rhs = obj as DataID;
if (message.Equals(rhs))
return true;
else return false;
}
}
class Data
{
private int number;
private string name;
private DataID id;
public Data(int number, string name, DataID id)
{
this.number = number;
this.name = name;
this.id = id;
}
public override string ToString()
{
return "ID:" + id.ToString() + " " + "number:" + this.number.ToString() + " " + "name:" + name;
}
}
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable(31);
DataID[] id = new DataID[10];
Data[] d = new Data[10];
for(int i = 0;i<10;i++)
{
id[i] = new DataID("Test" + i.ToString());
d[i] = new Data(i, "Name" + i.ToString(), id[i]);
ht.Add(id[i], d[i]);
Console.WriteLine(d[i].ToString());
}
Console.WriteLine("This Program is used to test hash table function.");
Console.WriteLine("Now we have already create a Hash table about data array,just input the key value of data you want to find, hash table will automatically find the matched value.");
Console.WriteLine("Hash table has " + ht.Count + " records");
while (true)
{
Console.WriteLine("Please input key.");
string input = Console.ReadLine();
if ("x" == input)
break;
DataID key = new DataID(input);
object result;
result = ht[key];
if (null == result)
Console.WriteLine("Cannot find such result!");
else
Console.WriteLine(((Data)result).ToString());
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace DictionaryTest
{
class DataID
{
private string message;
public DataID(string message)
{
this.message = message;
}
public override string ToString()
{
return message;
}
public override int GetHashCode()
{
return message.GetHashCode();
}
public override bool Equals(object obj)
{
DataID rhs = obj as DataID;
if (message.Equals(rhs))
return true;
else return false;
}
}
class Data
{
private int number;
private string name;
private DataID id;
public Data(int number, string name, DataID id)
{
this.number = number;
this.name = name;
this.id = id;
}
public override string ToString()
{
return "ID:" + id.ToString() + " " + "number:" + this.number.ToString() + " " + "name:" + name;
}
}
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable(31);
DataID[] id = new DataID[10];
Data[] d = new Data[10];
for(int i = 0;i<10;i++)
{
id[i] = new DataID("Test" + i.ToString());
d[i] = new Data(i, "Name" + i.ToString(), id[i]);
ht.Add(id[i], d[i]);
Console.WriteLine(d[i].ToString());
}
Console.WriteLine("This Program is used to test hash table function.");
Console.WriteLine("Now we have already create a Hash table about data array,just input the key value of data you want to find, hash table will automatically find the matched value.");
Console.WriteLine("Hash table has " + ht.Count + " records");
while (true)
{
Console.WriteLine("Please input key.");
string input = Console.ReadLine();
if ("x" == input)
break;
DataID key = new DataID(input);
object result;
result = ht[key];
if (null == result)
Console.WriteLine("Cannot find such result!");
else
Console.WriteLine(((Data)result).ToString());
}
Console.ReadKey();
}
}
}
...全文
相关推荐
解决python中遇到字典里key值为None的情况,取不出来的问题
原因可能是多方面的,比如字典的创建和访问发生在不同的作用域,或者在处理字典时存在一些逻辑错误。针对这个问题,我们可以采用以下策略来解决:1.
字典序问题源码
该项目基于MFC框架实现了一个对话框应用程序,旨在解决字典序相关问题。核心功能通过VC++开发,利用预编译头加快编译速度,并集成Windows资源如对话框、控件等。项目包含完整的资源定义和目标平台版本
python字典序问题实例
本文主要讨论了Python中字典序问题的实例,即如何对按照特定顺序排列的字母字符串进行编码,这个顺序要求字符按字母表顺序排列,且每个字符只出现一次。在给定的问题中,目标是生成所有长度不超过6的升序字符
字典序问题解析[源码]
字典序问题通常是计算机科学和算法竞赛中的一个常见问题,它要求计算一个字符串在所有可能字符串按字典序排列的序列中的位置。在处理这类问题时,算法设计者通常会采用不同的方法来优化计算过程,以便提高计算效率。
acm 上有关排列的字典序问题
在ACM竞赛中,字典序问题是一种常见的算法题型,尤其在排列组合的问题中经常出现。本题目的目标是给定一个排列,计算它的字典序值,并找到按照字典序排列的下一个排列。首先,我们需要理解什么是字典序。


