我在C#中有问题的HashSet ....中的Hashset C#是给人一种奇怪的行为
这是我的代码:
List<int> elements = new List<int>();
for(int i = 0;i< 100000;i++)
{
elements.Add(i);
}
HashSet<int> c = new HashSet<int>();
foreach(var ele in elements)
{
c.Add(ele);
}
Console.WriteLine("Working HashSet " + c.Count);
var Numbers = new HashSet<int>();
var mycount = 0;
using (TextReader reader = File.OpenText(@"myfile.txt"))
{
while ((line = reader.ReadLine()) != null)
{
mycount++;
int parsed = int.Parse(line);
Numbers.Add(parsed);
}
}
Console.WriteLine("my counter took" + mycount);
Console.WriteLine("Bad HashSet" + Numbers.Count);
工作的HashSet 100 000
我反了500 000
坏的HashSet 9999
为什么第二HashSet中不加入500 00 0项目???? 这对我来说是一个神秘的东西
'myfile.txt'中有多少*个不同的*号? – AakashM 2012-01-13 11:27:52
这里没有比较,在txt文件中是什么? Numbers.Add(mycount);'给你那个数字! – V4Vendetta 2012-01-13 11:29:51
试试'myCount + =(Numbers.Add(parsed))? 1:0'而不是'myCount ++' – Joe 2012-01-13 12:10:15