溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#中怎么利用foreach遍歷刪除集合中的元素

發(fā)布時間:2021-08-06 16:28:09 來源:億速云 閱讀:124 作者:Leah 欄目:編程語言

這篇文章給大家介紹C#中怎么利用foreach遍歷刪除集合中的元素,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

方法一:采用for循環(huán),并且從尾到頭遍歷

如果從頭到尾正序遍歷刪除的話,有些符合刪除條件的元素會成為漏網(wǎng)之魚;

正序刪除舉例:

List<string> tempList = new List<string>() { "a","b","b","c" }; for (int i = 0; i < tempList.Count; i++) {  if (tempList[i] == "b")  {   tempList.Remove(tempList[i]);  } } tempList.ForEach(p => {  Console.Write(p+","); });

控制臺輸出結(jié)果:a,b,b,c

有兩個2沒有刪除掉;

這是因為當i=1時,滿足條件執(zhí)行刪除操作,會移除第一個b,接著第二個b會前移到第一個b的位置,即游標1對應(yīng)的是第二個b。

接著遍歷i=2,也就跳過第二個b。

用for倒序遍歷刪除,從尾到頭

List<string> tempList = new List<string>() { "a","b","b","c" }; for (int i = tempList.Count-1; i>=0; i--) {  if (tempList[i] == "b")  {   tempList.Remove(tempList[i]);  } } tempList.ForEach(p => {  Console.Write(p+","); });

控制臺輸出結(jié)果:a,c,

這次刪除了所有的b;

方法二:使用遞歸

使用遞歸,每次刪除以后都從新foreach,就不存在這個問題了;

static void Main(string[] args) {  List<string> tempList = new List<string>() { "a","b","b","c" };  RemoveTest(tempList);  tempList.ForEach(p => {   Console.Write(p+",");  }); } static void RemoveTest(List<string> list) {  foreach (var item in list)  {   if (item == "b")   {    list.Remove(item);    RemoveTest(list);    return;   }  } }

控制臺輸出結(jié)果:a,c,

正確,但是每次都要封裝函數(shù),通用性不強;

方法三:通過泛型類實現(xiàn)IEnumerator

static void Main(string[] args) {  RemoveClass<Group> tempList = new RemoveClass<Group>();  tempList.Add(new Group() { id = 1,name="Group1" }) ;  tempList.Add(new Group() { id = 2, name = "Group2" });  tempList.Add(new Group() { id = 2, name = "Group2" });  tempList.Add(new Group() { id = 3, name = "Group3" });  foreach (Group item in tempList)  {   if (item.id==2)   {    tempList.Remove(item);   }  }  foreach (Group item in tempList)  {   Console.Write(item.id+",");  } //控制臺輸出結(jié)果:1,3

public class RemoveClass<T> {  RemoveClassCollection<T> collection = new RemoveClassCollection<T>();  public IEnumerator GetEnumerator()  {   return collection;  }  public void Remove(T t)  {   collection.Remove(t);  }  public void Add(T t)  {   collection.Add(t);  } } public class RemoveClassCollection<T> : IEnumerator {  List<T> list = new List<T>();  public object current = null;  Random rd = new Random();  public object Current  {   get { return current; }  }  int icout = 0;  public bool MoveNext()  {   if (icout >= list.Count)   {    return false;   }   else   {    current = list[icout];    icout++;    return true;   }  }  public void Reset()  {   icout = 0;  }  public void Add(T t)  {   list.Add(t);  }  public void Remove(T t)  {   if (list.Contains(t))   {    if (list.IndexOf(t) <= icout)    {     icout--;    }    list.Remove(t);   }  } }

關(guān)于C#中怎么利用foreach遍歷刪除集合中的元素就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI