读书札记: “集合已修改;可能无法执行枚举操作。”
已知条件:
string [] ch1= new string[3]{",155,",",160,",",155,"}
string [] ch2 = new string[9]{",203,",",202,",",199,",",199,",",155,",",195,",",188,",",160,",",168,"}
想要得到的字符串数组:
string [] ch = new string[6]{",203,",",202,",",199,",",195,",",188,",",168,"}
解决过程
1、把ch1相同的字符串删除,留下一个 ch1={",155,",",160,"}
2、把ch2相同的字符串删除,留下一个 ch2={",203,",",202,",",199,",",155,",",195,",",188,",",160,",",168,"}
3、比较ch1和ch2两数组,删除ch2中与ch1相同的字符串
------------------------------- foreach -------------------------------
string[] ch1 = new string[3] { ",155,", ",160,", ",155," };
string[] ch2 = new string[9] { ",203,", ",202,", ",199,", ",199,", ",155,", ",195,", ",188,", ",160,", ",168," };
List <string> list = new List <string>();
list.AddRange(ch1);
List <string> list2 = new List <string>();
list2.AddRange(ch2);
foreach (string s in list2)
{
if (list.Contains(s))
list2.Remove(s);
}
string[] ch = list2.ToArray();
问题:
foreach 内部使用枚举器迭代,而这个迭代器在一开始的时候就返回了,它的迭代次数已经确定了,
但是,你后面又更新了集合,导致之前的迭代次数不一致了
这样会出现list2
“集合已修改;可能无法执行枚举操作。”,无法继续下去啊
解决方案:
-------------------------方法一: for 循环实现 ---------------------------
//把ch1相同的字符串为空
for (int i = 0; i < ch1.Length; i++)
{
for (int j = i + 1; j < ch1.Length; j++)
{
if (ch1[i] == ch1[j])
{
ch1[j] = "";
}
}
}
// 把ch2相同的字符串为空
for (int i = 0; i < ch2.Length; i++)
{
for (int j = i + 1; j < ch2.Length; j++)
{
if (ch2[i] == ch2[j])
{
ch2[j] = "";
}
}
}
//比较ch1和ch2两数组,将ch2相同的字符为空
int m = 0;
for (int c = 0; c < ch2.Length; c++)
{
for (int p = 0; p < ch1.Length; p++)
{
if (ch2[c] == ch1[p])
{
ch2[c] = "";
m++;
}
}
}
//将ch2为空的字符移除
List<string> list = new List<string>();
list.AddRange(ch2);
for (int k = 0; k < m; k++)
{
if (list.Contains(""))
{
list.Remove("");
}
}
string[] ch = list.ToArray();
-------------------------方法二: C#3.0 code -------------------------
string[] ch1= new string[3]{",155,",",160,",",155,"};
string[] ch2 = new string[9] { ",203,", ",202,", ",199,", ",199,", ",155,", ",195,", ",188,", ",160,", ",168," };
string[] tempCh1 = ch1.Except(ch2).ToArray();
string[] tempCh2 = ch2.Except(ch1).ToArray();
ch1 = tempCh1;
ch2 = tempCh2;
------------------------方法三: C#3.0 code --------------------------
Dictionary<string, int> d1 = new Dictionary<string, int>();
foreach (string item in ch1)
d1[item] = 0; //自动识别Key的唯一性,并且为其自增长视图ID,所有的Value=0
//Key={",155,",",160,"}
//ID={0,1}
Dictionary<string, int> d2 = new Dictionary<string, int>();
foreach (string item in ch2)
d2[item] = 0;//自动识别Key的唯一性,并且为其自增长视图ID,所有的Value=0
//Key={",203,",",202,",",199,",",155,",",195,",",188,",",160,",",168,"}
//ID={0,1,2,3,4,5,6,7}
List<string> list1 = new List<string>();
foreach (string item in d1.Keys)
if (!d2.ContainsKey(item)) //是否包含具有指定键的项
list1.Add(item); //没有包含,就把这个值付给list1
List<string> list2 = new List<string>();
foreach (string item in d2.Keys)
if (!d1.ContainsKey(item)) //是否包含具有指定键的项
list2.Add(item); //没有包含,就把这个值付给list2
ch1 = list1.ToArray();
ch2 = list2.ToArray(); //数据压入
日期 未审核会场
2009-5-15 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,
2009-5-16 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,
2009-5-17 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,,160,,
2009-5-18 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,,160,,,155,,
日期 未审核会场
2009-5-15 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,
2009-5-16 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,
2009-5-17 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,,160,,
2009-5-18 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,,160,,,155,,
日期 未审核会场
2009-5-15 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,
2009-5-16 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,
2009-5-17 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,,160,,
2009-5-18 ,203,,,202,,,199,,,198,,,196,,,195,,,194,,,192,,,190,,,189,,,188,,,187,,,186,,,185,,,184,,,183,,,182,,,181,,,206,,,180,,,179,,,178,,,177,,,176,,,175,,,174,,,173,,,172,,,171,,,170,,,165,,,169,,,168,,,167,,,166,,,162,,,161,,,160,,,155,,