内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

C#中怎么利用foreach遍歷刪除集合中的元素-創(chuàng)新互聯(lián)

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

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

方法一:采用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+","); });

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

正確,但是每次都要封裝函數(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+",");  } //控制臺輸出結果: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遍歷刪除集合中的元素就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)站題目:C#中怎么利用foreach遍歷刪除集合中的元素-創(chuàng)新互聯(lián)
瀏覽地址:http://www.rwnh.cn/article12/ceidgc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、定制網(wǎng)站、云服務器小程序開發(fā)、標簽優(yōu)化網(wǎng)站收錄

廣告

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

成都網(wǎng)頁設計公司
堆龙德庆县| 昭苏县| 青冈县| 石泉县| 新乡市| 合江县| 原阳县| 高平市| 淮阳县| 土默特左旗| 新乡市| 长丰县| 昌乐县| 长垣县| 郁南县| 屏山县| 霍邱县| 乌鲁木齐县| 许昌市| 和龙市| 汝城县| 黄梅县| 禹城市| 漠河县| 灵璧县| 新源县| 通化市| 郧西县| 阿拉尔市| 富阳市| 专栏| 绵竹市| 阳信县| 广灵县| 商丘市| 台湾省| 娱乐| 淅川县| 年辖:市辖区| 徐水县| 色达县|