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. iterating arraylists

iterating arraylists

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 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.
  • N Offline
    N Offline
    numbers1thru9
    wrote on last edited by
    #1

    I normally iterate through arraylists with a foreach loop. However im iterating to search for a certain item to remove from the arraylist and i cant iterate and remove the item when its found in the foreach loop becasue it throws an exception. my arraylist is holding a list of listview items and im going to use a for loop or while loop to remove it from the arraylist. my problem is that i dont know how to iterate through arraylists and extract a single item when im not using a foreach loop. can someone help me get the right syntax for this?

    E P 2 Replies Last reply
    0
    • N numbers1thru9

      I normally iterate through arraylists with a foreach loop. However im iterating to search for a certain item to remove from the arraylist and i cant iterate and remove the item when its found in the foreach loop becasue it throws an exception. my arraylist is holding a list of listview items and im going to use a for loop or while loop to remove it from the arraylist. my problem is that i dont know how to iterate through arraylists and extract a single item when im not using a foreach loop. can someone help me get the right syntax for this?

      E Offline
      E Offline
      ejuanpp
      wrote on last edited by
      #2

      A solution may be copying the arraylist and iteration over the copied version :)

      N 1 Reply Last reply
      0
      • N numbers1thru9

        I normally iterate through arraylists with a foreach loop. However im iterating to search for a certain item to remove from the arraylist and i cant iterate and remove the item when its found in the foreach loop becasue it throws an exception. my arraylist is holding a list of listview items and im going to use a for loop or while loop to remove it from the arraylist. my problem is that i dont know how to iterate through arraylists and extract a single item when im not using a foreach loop. can someone help me get the right syntax for this?

        P Offline
        P Offline
        Private_Void
        wrote on last edited by
        #3

        Try this. using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace Assistant { class Program { static void Main(string[] args) { ArrayList al = new ArrayList(4); al.Add("Val1"); al.Add("Val2"); al.Add("Val3"); al.Add("Val4"); int iListLen = al.Count; for (int i = 0; iListLen > i; i++) { if (al[i].ToString() == "Val3") { al.RemoveAt(i); break; } } } } }

        N 1 Reply Last reply
        0
        • E ejuanpp

          A solution may be copying the arraylist and iteration over the copied version :)

          N Offline
          N Offline
          numbers1thru9
          wrote on last edited by
          #4

          I never thought about doing that and it worked perfectly, and was extremely simple. Thanks!!! :-D

          1 Reply Last reply
          0
          • P Private_Void

            Try this. using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace Assistant { class Program { static void Main(string[] args) { ArrayList al = new ArrayList(4); al.Add("Val1"); al.Add("Val2"); al.Add("Val3"); al.Add("Val4"); int iListLen = al.Count; for (int i = 0; iListLen > i; i++) { if (al[i].ToString() == "Val3") { al.RemoveAt(i); break; } } } } }

            N Offline
            N Offline
            numbers1thru9
            wrote on last edited by
            #5

            Thanks for your help too :-D

            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