这里是我的代码:如何循环两个列表后,如何在一次迭代后停止内循环?
List<int> myValues = new List<int>(new int[] { 5, 9, 3, 4, 7, 12, 0, 15 });
List<int> newOnly = new List<int>(new int[] { 5, 7, 1, 11, 7, 19, 76, 18 });
List<int> newValues = new List<int>();
for (int i = 0; i < myValues.Count; i++){
for (int x = 0; x < newOnly.Count; x++){
if (!myValues.Contains(newOnly[x])){
newValues.Add(newOnly[x]);
}
}
}
我要检查,如果在newOnly
该项目在myValues
已经存在一次。现在最后我得到newValues
40个项目,我应该只有9.
要开始....为什么你迭代myValues而不只是newOnly? –
删除并且使用LINQ。 –
查看“break”关键字:http://msdn.microsoft.com/en-us/library/adbctzc4.aspx – TyCobb