iterating arraylists
-
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?
-
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?
-
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?
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; } } } } }
-
I never thought about doing that and it worked perfectly, and was extremely simple. Thanks!!! :-D
-
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; } } } } }
Thanks for your help too :-D