欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#在foreach遍歷刪除集合中元素的三種實(shí)現(xiàn)方法

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

前言

在foreach中刪除元素時(shí),每一次刪除都會(huì)導(dǎo)致集合的大小和元素索引值發(fā)生變化,從而導(dǎo)致在foreach中刪除元素時(shí)會(huì)拋出異常。

集合已修改;可能無(wú)法執(zhí)行枚舉操作。

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

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

正序刪除舉例:

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

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

有兩個(gè)2沒(méi)有刪除掉;

這是因?yàn)楫?dāng)i=1時(shí),滿(mǎn)足條件執(zhí)行刪除操作,會(huì)移除第一個(gè)b,接著第二個(gè)b會(huì)前移到第一個(gè)b的位置,即游標(biāo)1對(duì)應(yīng)的是第二個(gè)b。

接著遍歷i=2,也就跳過(guò)第二個(gè)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+",");
 });

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

這次刪除了所有的b;

方法二:使用遞歸

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

 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;
   }
  }
 }

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

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

方法三:通過(guò)泛型類(lèi)實(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+",");
  }
 //控制臺(tái)輸出結(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);
   }
  }
 }

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)我們的支持。

上一篇:C#關(guān)鍵字async/await用法

欄    目:C#教程

下一篇:C# List介紹及具體用法

本文標(biāo)題:C#在foreach遍歷刪除集合中元素的三種實(shí)現(xiàn)方法

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4586.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有