Dictionary,int>如何查找?

abcfy2 2009-04-16 11:11:54
哈希表中存储的Tkey是List<byte>型,如何才能查找到?
Contains()方法是不行的,只能确定是否引用的同一个对象,没法确定值。
或者无法实现List<byte>实现byte[]的查找也行。
望高手赐教
...全文
570 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2009-04-17
  • 打赏
  • 举报
回复

Dictionary<byte[], int> dic = new Dictionary<byte[], int>();
byte[] b = new byte[2];
b[0] = 12;
b[1] = 23;
dic.Add(b, 200);

byte[] b1 = new byte[8];
b1[0] = 12;
b1[1] = 23;

Response.Write(isExistsKey(dic, b1));

private bool isExistsKey(Dictionary<byte[], int> dic, byte[] b)
{
string _bStr, bStr;
foreach (byte[] _b in dic.Keys)
{
_bStr = Encoding.UTF8.GetString(_b);
bStr = Encoding.UTF8.GetString(b);
_bStr = _bStr.Contains("\0") ? _bStr.Substring(0, _bStr.IndexOf("\0")) : _bStr;
bStr = bStr.Contains("\0") ? bStr.Substring(0, bStr.IndexOf("\0")) : bStr;
if (_bStr == bStr)
{
return true;
}
}
return false;
}


只是值的判断,没有判断长度是否一致
abcfy2 2009-04-17
  • 打赏
  • 举报
回复
都是不错的方法,结贴了。
加到一百分,散分
yongyinmg 2009-04-16
  • 打赏
  • 举报
回复

Dictionary<List<byte>, int> dic = new Dictionary<List<byte>, int>();
List<byte> listByte = new List<byte>();
//假设字典dic里的一个键值对的索引是listByte,值是3
if(dic.Contains(new KeyValuePair<List<byte>,int>(listByte,3)))
{
Response.Write("存在");
}
else
{
Response.Write("不存在");
}
abcfy2 2009-04-16
  • 打赏
  • 举报
回复
楼上两位先感谢一下,原来是重写了得到哈希校验码的方法和比较相等的方法,先尝试一下。
LemIST 2009-04-16
  • 打赏
  • 举报
回复
思路:Dictionary 里面Contains和TryGetValue都是比较Key值,先比较Key的hash code, 如果hash code相同,则比较equals().
将List<byte>的GetHashCode, 与Equals()都通过list包含的元素比较就可以实现你要的功能了。
LemIST 2009-04-16
  • 打赏
  • 举报
回复
public static void Main()
{
Dictionary<ListBytes, int> dic = new Dictionary<ListBytes, int>();
ListBytes bytes = new ListBytes();
bytes.Add(12);
bytes.Add(23);
dic.Add(bytes, 233);
ListBytes bytes2 = new ListBytes();
bytes2.Add(12);
bytes2.Add(23);
int value = 0;
if (dic.TryGetValue(bytes2, out value))
Console.WriteLine(value);

}

public class ListBytes : List<byte>
{
public override int GetHashCode()
{
int hash = 0;
for (int i = 0; i < this.Count; i++)
hash += this[i].GetHashCode();
return hash;
}

public override bool Equals(object obj)
{
if (!(obj is List<byte>))
return false;
List<byte> tar = obj as List<byte>;
if (tar.Count != this.Count)
return false;
for (int i = 0; i < tar.Count; i++)
if (this[i] != tar[i])
return false;
return true;
}
}
yangqidong 2009-04-16
  • 打赏
  • 举报
回复
Contains是可以的,有另外一个重载形式,再提供一个comparer参数就可以了
abcfy2 2009-04-16
  • 打赏
  • 举报
回复
写的是LZW中的字典,暂时考虑到哈希表查找快,才用到了Dictionary泛型,值类型的话,要存在byte转为string的问题,那么具体怎么实现呢?
justindreams 2009-04-16
  • 打赏
  • 举报
回复
Dictionary这样的东西,最好是用值类型作key,或者string作key
abcfy2 2009-04-16
  • 打赏
  • 举报
回复
具体怎么实现的?
可否讲解一下呢?
阿非 2009-04-16
  • 打赏
  • 举报
回复
引用类型,没办法直接查找。(同一引用除外)
你可以把List <byte> 或 byte[] 转成string 来进行 值的判断,
如果一致则是查找的对象
LemIST 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 yongyinmg 的回复:]
摘自msdn

HTML code
Dictionary<(Of <(TKey, TValue>)>)..::.Contains 方法
Dictionary<(Of <(TKey, TValue>)>) 类 请参见 发送反馈意见


重载列表
名称 说明
Contains(KeyValuePair<(Of <(UTP, UTP>)>)) 通过使用默认的相等比较器确定序列是否包含指定的元素。 (由 Enumerable 定义。)
Contains(KeyValuePair<(Of <(UTP, UTP>)>), IEqualityComparer<(Of <(KeyValuePair<(Of <(UTP, UTP>)>)>)>…
[/Quote]
这是2008里面的扩展函数。2005没有的。
sushou2009 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yongyinmg 的回复:]
引用 10 楼 abcfy2 的回复:
另外9L,Dictionary <>里面没有Contains方法,只有ContainsKey和ContainsValue方法,而且重载形式也没有那个形式的,能麻烦您重写一个较为完整的么?
最好调试一下,非常感谢。

解决了我一个大问题,50分不够了,明天我将追加分,期间关注此帖


Dictinary <>里确实有Contain这个方法,我上面的代码是经过调试的,Contain()里面是一个键值对的类型,即KeyValuePair,
请仔细在代码里看一下
[/Quote]
UP
yongyinmg 2009-04-16
  • 打赏
  • 举报
回复
摘自msdn

Dictionary<(Of <(TKey, TValue>)>)..::.Contains 方法
Dictionary<(Of <(TKey, TValue>)>) 类 请参见 发送反馈意见


重载列表
名称 说明
Contains(KeyValuePair<(Of <(UTP, UTP>)>)) 通过使用默认的相等比较器确定序列是否包含指定的元素。 (由 Enumerable 定义。)
Contains(KeyValuePair<(Of <(UTP, UTP>)>), IEqualityComparer<(Of <(KeyValuePair<(Of <(UTP, UTP>)>)>)>)) 通过使用指定的 IEqualityComparer<(Of <(T>)>) 确定序列是否包含指定的元素。 (由 Enumerable 定义。)
yongyinmg 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 abcfy2 的回复:]
另外9L,Dictionary <>里面没有Contains方法,只有ContainsKey和ContainsValue方法,而且重载形式也没有那个形式的,能麻烦您重写一个较为完整的么?
最好调试一下,非常感谢。

解决了我一个大问题,50分不够了,明天我将追加分,期间关注此帖
[/Quote]

Dictinary<>里确实有Contain这个方法,我上面的代码是经过调试的,Contain()里面是一个键值对的类型,即KeyValuePair,
请仔细在代码里看一下
abcfy2 2009-04-16
  • 打赏
  • 举报
回复
十分感谢6L,非常聪明的方法,暂时解决了难题。
另外9L,Dictionary<>里面没有Contains方法,只有ContainsKey和ContainsValue方法,而且重载形式也没有那个形式的,能麻烦您重写一个较为完整的么?
最好调试一下,非常感谢。

解决了我一个大问题,50分不够了,明天我将追加分,期间关注此帖

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧