案例:
我正在向我的arraylist添加对象,在我将对象添加到我的arraylist之前,我检查是否已经存在相同的对象。之后,我尝试将完整的arraylist重写为文本文件。
代码:
WaardeObjecten obj = new WaardeObjecten(index.ToString(), allFiles[index].ToString(), values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], values[10], values[11], values[12], values[13], values[14], values[15], values[16], values[17]);
//Check if duplcatie
foreach (WaardeObjecten item in listOfWaardeObjecten)
{
if (item.getIndex() == obj.getIndex())
{
listOfWaardeObjecten.Remove(item);
}
}
//Add to arraylist
listOfWaardeObjecten.Add(obj);
//writedata to log file
writeLogFile();
将所有arraylist元素写入.txt文件的代码:
//Creating a streamwriter to write to the file with the path of logFileName.
using (FileStream fs = new FileStream(logFileName, FileMode.Truncate, FileAccess.Write))
using (var sw = new StreamWriter(fs))
{
foreach (WaardeObjecten item in listOfWaardeObjecten)
{
sw.WriteLine(item.getTotalString());
}
}
现在我收到以下错误:
“mscorlib.dll中发生了'System.InvalidOperationException'类型的未处理异常。”
“附加信息:集合已更改。可能无法执行库存操作。”
更新
我现在使用的代码删除重复项:
//Check if duplcatie
for (int i = listOfWaardeObjecten.Count - 1; i >= 0; i--)
{
if (i == obj.getIndex())
{
listOfWaardeObjecten.RemoveAt(i);
}
}
//Add to arraylist
listOfWaardeObjecten.Add(obj);
//writedata to log file
writeLogFile();
用于编写文本文件的代码:
//Creating a streamwriter to write to the file with the path of logFileName.
using (FileStream fs = new FileStream(logFileName, FileMode.Truncate, FileAccess.Write))
using (var sw = new StreamWriter(fs))
{
foreach (WaardeObjecten item in listOfWaardeObjecten)
{
sw.WriteLine(item.getTotalString());
}
}
输出如果我首先使用所有不同的值执行10-15个图像,我将回放到第一个图像并将所有值放在“N.v.t。”上:
2. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm04.JPG : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
4. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm06.JPG : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
6. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm08.JPG : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
8. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\Detail tbv blisemafleiding A-1101.jpg : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
10. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\IMG-20131108-01010.jpg : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
1. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm02.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
3. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm05.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
5. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm07.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
7. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\beschadigde isolatie.jpg : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
9. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\Foto tbv bliksemafleiding 1.jpg : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
11. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\C-1106-final\99_History\Inspecties\DSC00864.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
12. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\C-1107-fnal\99_History\Inspecties\DSC00864.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
13. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\H-1104-final\99_History\Inspecties\100_0671.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
14. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\P-1111-final\99_History\Onderhouds rapportages\2014 wk50 div schades\P-1111 eerste schadepomp 2.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
正如您所看到的,并非所有值都是对“N.v.t。”的更新。
希望任何人都可以提供帮助!
答案 0 :(得分:5)
在集合上使用foreach
循环时,无法修改/删除集合中的项目。但是如果你想从集合中删除项目,那么请改用for
循环。
for (int i = listOfWaardeObjecten.Count-1; i >= 0; i--)
{
if (i == obj.getIndex())
{
listOfWaardeObjecten.RemoveAt(i);
}
}
如果您不想使用for循环,则可以使用break
语句和foreach
循环。 See here