這裏是我的代碼:如何循環兩個列表後,如何在一次迭代後停止內循環?
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