【问题标题】:Iteration in Dictionary字典中的迭代
【发布时间】:2013-02-01 18:05:49
【问题描述】:

我有一本字典,我必须检查一个条件直到字典的最后一个元素。我使用了 movenext 方法,但它抛出异常。我想做的是当数组 B 中的元素出现时。将每个元素与 A 中的元素进行比较直到最后一个大于 B 的元素,然后删除满足条件的键值对。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;

namespace WellPuzzle
{

    class Solution
    {
        public void falling_disks(int[] A, int[] B)
        {

           Dictionary<int, int> d1 = new Dictionary<int, int>();

            List<KeyValuePair<int, bool>> list = new List<KeyValuePair<int, bool>>();

            var enum1 = d1.GetEnumerator();
            int count = 0;
            for (int i = 0; i <= A.Length - 1; i++)
            {
                //h1.Add(count++,A[i]);
                d1.Add(count++, A[i]);
            }

            foreach (int ele in B)
            {

                foreach (KeyValuePair<int, int> item in d1)
                {
                    var pair = item.Value;

                    if (ele <=pair && (enum1.MoveNext()!=null))
                    {
                        continue;
                    }
                    else if (ele <= pair && (enum1.MoveNext() == null))
                    {

                        list.Add(new KeyValuePair<int, bool>(pair, true));
                        d1.Remove(pair);
                    }
                    else
                    {
                        //add key of current pair as filled in second hashtable
                        //remove element from first hashtable
                        //iterate till last
                        list.Add(new KeyValuePair<int, bool>(pair, true));
                        d1.Remove(pair);
                    }
}
}
}
}







    class Program
    {
        static void Main(string[] args)
        {
            int[] A = new int[] { 5, 6, 4, 3, 6, 2, 3 };
            int[] B = new int[] { 2 };
            Solution s1 = new Solution();
            s1.falling_disks(A, B);
        }
    }
}

【问题讨论】:

  • 它抛出了什么异常?
  • @Anonymiser:我看到你在“使用”System.Linq。当然,那里已经有方法可以为您提供所需的结果。我很难理解你想要的确切结果。你能提供一个输入(我猜是AB)和输出吗?
  • @syedmohsin,显然“在迭代时修改了集合” - 正如预期的那样。现在,由于缺少问题,因此不清楚 Anonymiser 是否想要此异常或需要做其他事情。

标签: c# collections dictionary hashtable enumeration


【解决方案1】:

您不能在迭代枚举时修改它。如,

foreach(var item in someList)
{
   if (someCondition)
      someList.remove(item); // At run time you will get an exception saying that the collection was modified
}

在这些情况下,您通常会存储需要删除的东西的 ID,然后通过迭代新集合来删除它们,

var itemsToRemove = new List<int>();
foreach(var item in someList)
{
   if (someCondition)
      itemsToRemove.Add(item.Id);
}

foreach(var id in itemsToRemove)
{
   var item = someList.First(l => l.Id = id)
   someList.Remove(item);
}

【讨论】:

  • 但是@bassam....在迭代时我想知道我是否在特定条件下到达字典的最后一个元素。有人可以解释一下字典的movenext方法吗?...或者是否有一种方法可以到达满足条件的字典中的最后一个元素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多