Friday, February 22, 2008

Beware when removing items from an ArrayList

I have an arraylist, and each item contains a structure.  The structure contains a point and an index value.  There are times when I need to go through the arraylist and delete all the items which have an index equal to a specific value.  So for example, the arraylist contains the followint
 
0 -- pt = 1, index = 0
0 -- pt = 2, index = 0
0 -- pt = 3, index = 0
0 -- pt = 6, index = 1
0 -- pt = 9, index = 1
 
When I tried to loop through through the array using a For Loop, I had the problem where after deleting the first point, the second point was moved to the first position, and the for loop missed it.  This is the code I used for the For Loop:
 
  For i = 0 To aList.Count - 1
   If aList(i).Index = Index Then 'The Index was supplied to the function
    aList.RemoveAt(i)
   End If
  Next
 Furthermore, the for loop ends up giving you an out of bounds error, because the size of the array changes.  So my solution was to use a while loop, like so:
  While i < aList.Count - 1
   If aList(i).Index = Index Then 'The Index was supplied to the function
    aList.RemoveAt(i)
   Else
    i = i + 1
   End If
  End While
Now the first thing I asked when I did this is what happens if you have an arraylist like so:
0 -- pt = 1, index = 0
0 -- pt = 2, index = 1
0 -- pt = 3, index = 1
0 -- pt = 6, index = 0
0 -- pt = 9, index = 0
 
In this situation the arraylist will delete the first item, then i will still be equal to 0, and it will look at the the item with value pt = 2, it will keep it.  It will then look at pt = 3, it will keep it.  It will look at pt = 6, and delete it.  Then look at pt = 9 and delete it as well.  So it looks like this should work without any problems.  Though I'm sure there is something that I'm missing here.
 
 
 

No comments: