Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. using IEnumerator to removing items

using IEnumerator to removing items

Scheduled Pinned Locked Moved C#
tutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anderslundsgard
    wrote on last edited by
    #1

    I want to remove some items from an ArrayList. Can I do that using a foreach(…)- loop an IEnumerator like this. foreach(string s in arraylist) { if (s == “del”) { // Here I want to remove the item from arraylist… } } Any ideas how to solve this without creating a new ArrayList and add items I want to save. Feels like that solution will bee to time consuming… All tips are welcomed!. _____________________________ ...and justice for all APe

    V V 2 Replies Last reply
    0
    • A anderslundsgard

      I want to remove some items from an ArrayList. Can I do that using a foreach(…)- loop an IEnumerator like this. foreach(string s in arraylist) { if (s == “del”) { // Here I want to remove the item from arraylist… } } Any ideas how to solve this without creating a new ArrayList and add items I want to save. Feels like that solution will bee to time consuming… All tips are welcomed!. _____________________________ ...and justice for all APe

      V Offline
      V Offline
      Vikram A Punathambekar
      wrote on last edited by
      #2

      d00_ape wrote:

      I want to remove some items from an ArrayList.

      Check out ArrayList.Remove(). I think that will do the job for you. Cheers, Vikram.


      "Don't judge me You could be me in another life In another set of circumstances"   - "Tomorrow we'll see", Sting.

      A 1 Reply Last reply
      0
      • V Vikram A Punathambekar

        d00_ape wrote:

        I want to remove some items from an ArrayList.

        Check out ArrayList.Remove(). I think that will do the job for you. Cheers, Vikram.


        "Don't judge me You could be me in another life In another set of circumstances"   - "Tomorrow we'll see", Sting.

        A Offline
        A Offline
        anderslundsgard
        wrote on last edited by
        #3

        Yeah!! something like this: int iIndex = 0; while( iIndex < arrayList.Count) { if( //you need to remove this item) { arrayList.RemoveAt( iIndex); } else { iIndex++; } } _____________________________ ...and justice for all APe

        1 Reply Last reply
        0
        • A anderslundsgard

          I want to remove some items from an ArrayList. Can I do that using a foreach(…)- loop an IEnumerator like this. foreach(string s in arraylist) { if (s == “del”) { // Here I want to remove the item from arraylist… } } Any ideas how to solve this without creating a new ArrayList and add items I want to save. Feels like that solution will bee to time consuming… All tips are welcomed!. _____________________________ ...and justice for all APe

          V Offline
          V Offline
          vinSharp
          wrote on last edited by
          #4

          Hi APe, Whenever you need to remove any item/s from an ArrayList, and that too by looping then you always need to traverse the ArrayList in reverse order. This requirement cannot be achieved using an foreach statement. So, I think you would need to use the 'FOR' loop, as follows: for (int i = testList.Count-1; i >= 0; i--) { if(CONDITION is TRUE) { testList.RemoveAt(i); } } Regards, vinSharp There are two types of fools in this world: One who give advice and the others who do not take it...:laugh:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups