欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2019年12月17日 09:18:31   作者:willingtolove  
這篇文章主要給大家總結(jié)介紹了關(guān)于C#在foreach遍歷刪除集合中元素的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

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

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

方法一:采用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沒有刪除掉;

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

總結(jié)

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

相關(guān)文章

  • [C#].NET中幾種Timer的使用實例

    [C#].NET中幾種Timer的使用實例

    本篇文章主要介紹了.NET中幾種Timer的使用,具有一定的參考價值,有興趣的可以了解一下。
    2016-12-12
  • 淺談C#中的for循環(huán)與foreach循環(huán)

    淺談C#中的for循環(huán)與foreach循環(huán)

    本篇文章主要介紹了C#中的for循環(huán)與foreach循環(huán)的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-05-05
  • C#儀器數(shù)據(jù)文件解析Excel文件的方法淺析(xls、xlsx)

    C#儀器數(shù)據(jù)文件解析Excel文件的方法淺析(xls、xlsx)

    這篇文章主要給大家介紹了關(guān)于C#儀器數(shù)據(jù)文件如何解析Excel文件的方法,包括解析xls、xlsx兩種格式,文中介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • C#播放音頻文件的詳細(xì)步驟

    C#播放音頻文件的詳細(xì)步驟

    這篇文章主要介紹了C#播放音頻文件的詳細(xì)步驟,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • C# 程序集和反射詳解

    C# 程序集和反射詳解

    本文主要介紹了C# 程序集和反射的相關(guān)知識。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • C#實現(xiàn)GridView導(dǎo)出Excel實例代碼

    C#實現(xiàn)GridView導(dǎo)出Excel實例代碼

    本篇文章主要介紹了C#實現(xiàn)GridView導(dǎo)出Excel實例代碼,這里整理了詳細(xì)的代碼,非常具有實用價值,需要的朋友可以參考下。
    2017-03-03
  • C#判等對象是否相等的方法匯總

    C#判等對象是否相等的方法匯總

    這篇文章主要介紹了C#判等對象是否相等的方法匯總,非常實用,需要的朋友可以參考下
    2014-08-08
  • C#從字符串中指定位置移除子字符串的方法

    C#從字符串中指定位置移除子字符串的方法

    這篇文章主要介紹了C#從字符串中指定位置移除子字符串的方法,涉及C#中Remove方法的使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • C# 中的IComparable和IComparer的使用及區(qū)別

    C# 中的IComparable和IComparer的使用及區(qū)別

    這篇文章主要介紹了C# 中的IComparable和IComparer的使用及區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • SQL語句刪除和添加外鍵、主鍵的方法

    SQL語句刪除和添加外鍵、主鍵的方法

    本文將詳細(xì)介紹SQL語句刪除和添加外鍵、主鍵的方法,需要的朋友可以參考下
    2012-11-11

最新評論