【问题标题】:Iterate and modify Dictionary迭代和修改字典
【发布时间】: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


【解决方案1】:

你的问题很难理解。也许这就是您想要在循环开始时执行的操作?

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

【讨论】:

  • 我正在做 current.movenext()。但在迭代之后,我希望 current 指向字典中的第一个元素,其键、值为 (0,6)。但我们知道,一旦我们迭代字典,当我们执行 current.movenext().current 时,在字典中的最后一个元素之后将指向原始变量。
【解决方案2】:

如果我正确理解了您的问题,您将无法做到。枚举器使您可以连续访问集合,这就是重点。你不能突然将它移动到特定元素,而不从头开始迭代到那个元素。

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

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

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

【讨论】:

    【解决方案3】:

    我不确定我是否理解你的问题,但也许你想改变这个:

                    previous = current;
    

    到这里:

                    previous.MoveNext();
    

    这样,'previous' 将永远落后于'current' 一步。如果按照在原始代码中的方式分配变量,则只有两个对“当前”对象的引用,然后递增。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-10
      • 2021-07-01
      • 1970-01-01
      • 2023-04-06
      • 2011-10-14
      • 2014-05-29
      • 1970-01-01
      • 2021-04-05
      相关资源
      最近更新 更多