Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.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
Javascript 使用foreach从集合列表中移除vnig元素时获取枚举异常_Javascript_Java_C#_Selenium Webdriver - Fatal编程技术网

Javascript 使用foreach从集合列表中移除vnig元素时获取枚举异常

Javascript 使用foreach从集合列表中移除vnig元素时获取枚举异常,javascript,java,c#,selenium-webdriver,Javascript,Java,C#,Selenium Webdriver,当我试图使用foreach从列表集合中删除元素时,我得到了枚举异常。我在网络中搜索了,但我没有得到关于该问题的正确解释。如果有人知道此共享信息,则无法从当前运行foreach的同一ICollection中删除项目。如果要在循环中删除同一集合中的项,请使用辅助列表 //Define a secondary variable var items = new List<string>{"One", "Two", "Three",

当我试图使用foreach从列表集合中删除元素时,我得到了枚举异常。我在网络中搜索了,但我没有得到关于该问题的正确解释。如果有人知道此共享信息

,则无法从当前运行foreach的同一ICollection中删除项目。如果要在循环中删除同一集合中的项,请使用辅助列表

//Define a secondary variable
var items = new List<string>{"One", "Two", "Three", "Four"};
var secondaryList = new List<string>(items);

//Now iterate and remove according you want from second variable
foreach(var i in items)
{
  if(i=="Two") { secondaryList.remove(i) }
}

//Once iteration is over, copy back
items = new List<string>(secondaryList);
//定义一个辅助变量
变量项=新列表{“一”、“二”、“三”、“四”};
var secondaryList=新列表(项目);
//现在,根据需要从第二个变量中迭代并删除
foreach(项目中的var i)
{
if(i=“Two”){secondaryList.remove(i)}
}
//迭代结束后,复制回来
项目=新列表(第二个列表);

是的,以前的海报是正确的。不应从正在迭代的集合中删除项。它可能使迭代器无效。首先确定需要删除哪些项,然后遍历该列表以从原始集合中删除这些项。大概是这样的:

IList<int> myCollection = new List<int> { 1, 2, 3, 4, 5, 6 };
IList<int> itemsToRemove = new List<int>();

foreach(var item in myCollection)
{
    if (item % 2 == 0)
    {
        itemsToRemove.Add(item);
    }
}

foreach(var toRemove in itemsToRemove)
{
    myCollection.Remove(item);
}
IList myCollection=新列表{1,2,3,4,5,6};
IList itemsToRemove=新列表();
foreach(myCollection中的var项)
{
如果(项目%2==0)
{
itemsToRemove.Add(项目);
}
}
foreach(itemsToRemove中的var toRemove)
{
myCollection.移除(项目);
}

为什么会出现错误?这是设计好的。(用C#表示)

写作:

foreach (var item in list)
{
    // do stuff with item
}
语法糖用于:

var iterator = items.GetEnumerator();
while (iterator.MoveNext()
{
    var item = iterator.Current;
    // do stuff with item
}
最后用于列表的MoveNext实现是MoveNext()

如果我们看一下这个代码:

public bool MoveNext() 
{
    List<T> localList = list;

    if (version == localList._version && ((uint)index < (uint)localList._size)) 
    {                                                     
         current = localList._items[index];                    
         index++;
         return true;
    }
  return MoveNextRare();
}
在这里,检查大小和“版本”,这是一个在列表每次更改时都会递增的字段。因此,如果“本地”版本与“原始”版本不同,将引发InvalidOperationException

你可以在C#中解决这个问题:


这是java、javascript还是C#?请编辑标签,使其仅包含相关语言。也请阅读,并包括一个检查此二次列表引用相同的集合!这到底是如何工作的?次要列表不应该是对前一个变量的引用。它应该是一个仅包含复制内容的不同变量。感谢sangeethIf它对您有帮助,您可以将此答案标记为已接受。这将不起作用,您可能希望执行var secondaryList=新列表(项);在最后一步中,items=新列表(secondaryList)。
    private bool MoveNextRare()
                {                
                    if (version != list._version) {
                        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
                    }
     
                    index = list._size + 1;
                    current = default(T);
                    return false;                
                }
}
var items = new List<string>{"One", "Two", "Three", "Four"};
//we don't rely on iterating, but on indexed access to the list
//iterating from the last item to the first will not cause funkiness
for (int i = items.Count - 1; i >= 0; i--)
{
    if(items[i]=="Two") { items.RemoveAt(i) }
}
var items = List<string>{"One", "Two", "Three", "Four"};
var itemsToRemove = items.Where(item => item == "Two");
items = items.Except(itemsToRemove).ToList();
items.RemoveAll(item => item == "Two");