|
-
Feb 16th, 2007, 09:22 AM
#1
Thread Starter
Member
deleting from array list
Hi there,
I have this loop to delete elements from an arraylist
VB Code:
For i = 0 To myarraylist.count - 1
If some_statement Then
myarraylist.removeat(i)
End If
Next
Is there any risk of having index out of range, as the arraylist gets smaller??
Basicaly the question is if the compiler sets the number of loops at the beggining or checks the myarraylist.count - 1 after every loop...Thanks
-
Feb 16th, 2007, 01:29 PM
#2
Frenzied Member
Re: deleting from array list
Hi,
start at the end of the array, and loop 'backwards'
Pete
-
Feb 16th, 2007, 02:29 PM
#3
Re: deleting from array list
You will definitely go out of range unless you do as Pete suggested. If you remove even a single item, then the loop will run past the end of the array. The MyArrayList.Count expression is evaluated the first time only, so it doesn't shrink as the arraylist shrinks.
To loop the other way, start with X = MyArrayList.Count-1 to 0 Step -1
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|