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

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

創(chuàng)新互聯建站是一家專業(yè)提供廣漢企業(yè)網站建設,專注與成都網站制作、成都網站設計、html5、小程序制作等業(yè)務。10年已為廣漢眾多企業(yè)、政府機構等服務。創(chuàng)新互聯專業(yè)網絡公司優(yōu)惠進行中。

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

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

正序刪除舉例:

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+","); });

控制臺輸出結果:a,b,b,c

有兩個2沒有刪除掉;

這是因為當i=1時,滿足條件執(zhí)行刪除操作,會移除第一個b,接著第二個b會前移到第一個b的位置,即游標1對應的是第二個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+","); });

控制臺輸出結果: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;   }  } }

控制臺輸出結果:a,c,

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

方法三:通過泛型類實現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+",");  } //控制臺輸出結果: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);   }  } }

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

本文標題:C#中怎么利用foreach遍歷刪除集合中的元素
文章出自:http://bm7419.com/article20/jjccjo.html

成都網站建設公司_創(chuàng)新互聯,為您提供虛擬主機、商城網站做網站、微信小程序、網頁設計公司、App開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

成都網頁設計公司