Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 迭代和修改字典_C#_Asp.net_Dictionary_Hashtable_Ienumerator - Fatal编程技术网

C# 迭代和修改字典

C# 迭代和修改字典,c#,asp.net,dictionary,hashtable,ienumerator,C#,Asp.net,Dictionary,Hashtable,Ienumerator,我正在尝试为上解释的问题实施解决方案。 我对c#语言不熟悉。我想使用枚举器和特定条件迭代字典。因此有两个变量current和previous。current指向字典的第一个元素。previous指向字典中的前一个元素。在迭代字典时,我像foll一样迭代 previous=current; current.MoveNext(); 问题是,当我们第一次遍历整个字典时,前面的点指向字典中的最后一个元素,当前点指向随机键值对RawVariable(0,0)但现在,当我们第二次遍历字典时,我希望curr

我正在尝试为上解释的问题实施解决方案。 我对c#语言不熟悉。我想使用枚举器和特定条件迭代字典。因此有两个变量current和previous。current指向字典的第一个元素。previous指向字典中的前一个元素。在迭代字典时,我像foll一样迭代

previous=current;
current.MoveNext();
问题是,当我们第一次遍历整个字典时,前面的点指向字典中的最后一个元素,当前点指向随机键值对RawVariable(0,0)但现在,当我们第二次遍历字典时,我希望current指向字典中的第一个元素。如何使current指向具有特定键或值的某个元素

这是我的代码片段

 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;
    }

}
公共磁盘(int[]A,int[]B)
{
字典填充字典=填充字典(d1,A);
//previous在字典中存储上一个元素
var previous=filledDictionary.GetEnumerator();
//当前存储上一个元素的下一个元素
var current=filledDictionary.GetEnumerator();
current.MoveNext();
//对于数组B中的每个传入元素
foreach(B中的内部元素)
{
//检查当前键是否填充在哈希表h1中,即检查
//已添加
如果(!checkifthatvalueisfilled(current.current.Key))
{
//若不存在,则检查当前值是否小于或等于元素
而((current.current.Value>=ele))
{
//将上一个指定给当前
先前=当前;
//将当前位置移动到下一个位置
current.MoveNext();
}
listofitemstoremove.Add(previous.Current.Key);
}
其他的
{
listofitemstoremove.Add(current.current.Key);
}
foreach(listofitemstoremove中的int项)
{
如果(!(h1.容器(项目)))
h1.添加(项,真);
}
}
Console.WriteLine(listofitemstoremove.Capacity);
}
public bool checkifthatvalueisfilled(整数键)
{
if(h1.ContainsValue(h1.ContainsKey))==true)
返回true;
否则返回false;
}
}

你的问题很难理解。也许这就是您在循环开始时想要做的事情

current = h1.GetEnumerator();
current.MoveNext();

如果我没弄错你的问题,你就做不到。枚举器为您提供对集合的连续访问,这就是重点。如果不从头开始迭代到该元素,就不能突然将其移动到特定元素

此外,我看不到使用枚举器的任何好理由。如果需要对算法的上一个和当前元素进行引用,则应该存储它们的键,而不是枚举数。我也很确定这些线路

 while ((current.Current.Value >= ele))
            {
                //assign previous to current
                previous = current;
                //move current to next position
                current.MoveNext();
            }

a) 当您到达集合末尾时,将引发异常b)由于您正在分配引用类型,因此无法按预期工作

我不确定是否理解您的问题,但您可能希望更改此设置:

                previous = current;
为此:

                previous.MoveNext();

这样,“以前的”总是比“现在的”落后一步。如果您按照原始代码中的方式分配变量,您只需要对“当前”对象进行两次引用,然后将其递增。

您的问题目前还不清楚。您的代码使用了一些根本没有解释的变量和方法,并且您的文本解释很难理解。请澄清。一个猜测。。。如果(ele.Equals(b.Last()),则将
current
分配给开头的
if(ele.Equals(b.Last())
?是否要在
字典中为数组B中的每个元素搜索某个值
@Shekhar No.。我要检查该元素是否小于字典中的值。如果数组B中的元素大于字典中的特定值,我将在列表中添加该keyvaluepair。问题是,当我迭代时,我需要有两个指针rrent和previous。previous跟在current之后。现在,当current指向字典中的最后一个元素时。问题解释为我正在执行current.movenext()。但迭代后,我希望current指向字典中键为(0,6)的第一个元素但我们知道,一旦我们迭代字典,当我们执行current.movenext()时,在字典中的最后一个元素之后。current将指向原始变量。