99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

C#中怎么利用foreach遍歷刪除集合中的元素-創新互聯

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

創新互聯建站服務項目包括二道江網站建設、二道江網站制作、二道江網頁制作以及二道江網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,二道江網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到二道江省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!

方法一:采用for循環,并且從尾到頭遍歷

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

正序刪除舉例:

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時,滿足條件執行刪除操作,會移除第一個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://www.yijiale78.com/article12/ceidgc.html

成都網站建設公司_創新互聯,為您提供網站排名定制網站云服務器小程序開發標簽優化網站收錄

廣告

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

成都網頁設計公司