我正在尝试为http://users.metropolia.fi/~dangm/blog/?p=67上解释的问题实施解决方案。我是 C# 语言的新手。我想使用枚举器和特定条件遍历字典。所以有两个变量 current 和 previous.current 指向字典的第一个元素。previous 指向字典中的前一个元素。而迭代我正在迭代的字典
previous=current;
current.MoveNext();
问题是,当我们第一次遍历整个字典时,前一个点指向字典中的最后一个元素,当前点指向随机键值对 RawVariable(0,0)。但是现在,当我们第二次遍历字典时,我希望当前指向第一个元素在 dictionary.how 我如何使当前点指向具有特定键或值的某些元素
这是我的代码片段
public void falling_disks(int[] A, int[] B)
{
Dictionary<int, int> filledDictionary = filldictionary(d1, A);
//previous stores the previous element in dictionary
var previous = filledDictionary .GetEnumerator();
//current stores next element of previous
var current = filledDictionary .GetEnumerator();
current.MoveNext();
//for each incoming element in array B
foreach (int ele in B)
{
//check if the current key is filled in hashtable h1 that is check if it
//is already added
if (!checkifthatvalueisfilled(current.Current.Key))
{
//if not check if current value is less than or equal to element
while ((current.Current.Value >= ele))
{
//assign previous to current
previous = current;
//move current to next position
current.MoveNext();
}
listofitemstoremove.Add(previous.Current.Key);
}
else
{
listofitemstoremove.Add(current.Current.Key);
}
foreach (int item in listofitemstoremove)
{
if (!(h1.ContainsKey(item)))
h1.Add(item, true);
}
}
Console.WriteLine(listofitemstoremove.Capacity);
}
public bool checkifthatvalueisfilled(int key)
{
if (h1.ContainsValue(h1.ContainsKey(key)) == true)
return true;
else return false;
}
}