问题描述
|
Okai,我有以下方法:
public void Insert(SortedDictionary<byte[],uint> recs)
{
SortedDictionary<byte[],uint> records = new SortedDictionary(recs,myComparer);
}
我希望实现的是使用实现IComparer的\“ myComparer \”指定的新规则对\“ recs \”中的记录进行排序。这样做的确差不多,但是我被以下消息打中了异常:
具有相同键的条目
存在。
我想知道这怎么可能,因为\“ recs \”已经是具有约130k键的字典。
public int Compare(byte[] a,byte[] b)
{
return Inhouse.ByteConverter.ToString(a).Compareto(
Inhouse.ByteConverter.ToString(b));
}
(这只是一个片段。)
解决方法
如果\“ recs \”的比较器与注入记录的比较器不同,则可能会重复。也就是说,如果\“ recs \”通过对象引用进行比较,而myComparer比较实际字节,则将发生冲突。
,检查比较器代码:
SortedDictionary(Of
TKey,TValue)必须唯一
给指定的比较器;因此,源词典中的每个键也必须
根据指定的比较器唯一。
在您的新比较器中,两个具有正常
byte[]
比较的不同键可能会变得相等。
这就是msdn所说的...
,您必须在调用方法中使用相同的“ 3”对象。所以我想你的代码是这样的:
SortedDictionary<byte[],uint> dic = new SortedDictionary<byte[],uint>();
foreach (var thing in things)
{
dic.Clear();
Populate(dic);
Insert(dic);
}
应该是这样的:
SortedDictionary<byte[],uint>();
foreach (var thing in things)
{
dic = new SortedDictionary<byte[],uint>();
Populate(dic);
Insert(dic);
}
您可以张贴调用Insert
方法的代码吗?