【发布时间】:2013-02-22 04:17:11
【问题描述】:
我正在尝试解决http://users.metropolia.fi/~dangm/blog/?p=67 上说明的问题。 我是 C# 语言的新手。我想使用枚举器和特定条件遍历字典。所以有两个变量 current 和 previous.current 指向字典的第一个元素。previous 指向字典中的前一个元素。而迭代我正在像下面这样迭代的字典
previous=current;
current.MoveNext();
问题是当我们第一次遍历整个字典时,前一个点指向字典中的最后一个元素,当前点指向随机键值对 RawVariable(0,0)。但是现在,当我们第二次遍历字典时,我希望当前指向到字典中的第一个元素。我如何使当前指向具有特定键或值的某个元素
这是我的代码 sn-p
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;
}
}
【问题讨论】:
-
您的问题目前非常不清楚。您的代码使用了几个根本没有解释的变量和方法,并且您的文本解释很难理解。请澄清。
-
一个猜测...分配
current到开头if (ele.Equals(b.Last())? -
你想在
Dictionary中搜索一些值 -
@Shekhar 否。对于数组 BI 中的每个元素,要检查该元素是否小于字典中的值。如果数组 B 中的元素大于字典中的特定值,我将在list
.Problem 是当我迭代时我需要有 2 个指针 current 和 previous.Previous 跟随 current.Now 当 current 指向字典中的最后一个元素时。users.metropolia.fi/~dangm/blog/?p=67 上解释了问题
标签: c# asp.net dictionary hashtable ienumerator