I am trying via the following code to remove from the list all the values are equal to zero. However I am facing a strange proble. While I was running it step by step, the count of list decreases according to the item I remove each time. Fine up to here. I really don't understand a thing I am getting error because i is out of range. I am saying for i=0 to lst.count-1
the lst.count changes each time I remove an item. I don't understand how is it possible i=4 to access the loop :/ Could anyone explain me, what's going on here?
Dim lst As New ArrayList
lst.Add(5)
lst.Add(4)
lst.Add(0)
lst.Add(2)
lst.Add(0)
lst.Add(8)
Dim i As Integer
For i = 0 To lst.Count - 1
If lst(i) = 0 Then
lst.RemoveAt(lst(i))
End If
Next
However I know that the right code to do that is, but I really dont' understand, why it doesn't work with the previous way.
Any help will be much appreciated. Thank you so much in advanced.
Dim number As Integer
For Each number In lst
If number = 0 Then
lst.Remove(number)
End If
Next
the lst.count changes each time I remove an item. I don't understand how is it possible i=4 to access the loop :/ Could anyone explain me, what's going on here?
Dim lst As New ArrayList
lst.Add(5)
lst.Add(4)
lst.Add(0)
lst.Add(2)
lst.Add(0)
lst.Add(8)
Dim i As Integer
For i = 0 To lst.Count - 1
If lst(i) = 0 Then
lst.RemoveAt(lst(i))
End If
Next
However I know that the right code to do that is, but I really dont' understand, why it doesn't work with the previous way.
Any help will be much appreciated. Thank you so much in advanced.
Dim number As Integer
For Each number In lst
If number = 0 Then
lst.Remove(number)
End If
Next