daJman
asked on
Compare arrays of different sizes
I'm sure there is something very wrong with my thinking but I can't see it.
I'm trying to compare 2 arraylists of different sizes with:
If array1.Count > array2.Count Then
For i As Integer = 0 To array1.Count - 1
For x As Integer = 0 To array2.Count - 1
If array2.Item(x).ToString = array1.Item(i).ToString Then
array1.RemoveAt(i)
array1.RemoveAt(i)
End If
Next
Next
End if
It actually works well but I get errors:
System.ArgumentOutOfRangeE xception: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.ArrayLi st.get_Ite m(Int32 index) ...
Where is my problem?
I'm trying to compare 2 arraylists of different sizes with:
If array1.Count > array2.Count Then
For i As Integer = 0 To array1.Count - 1
For x As Integer = 0 To array2.Count - 1
If array2.Item(x).ToString = array1.Item(i).ToString Then
array1.RemoveAt(i)
array1.RemoveAt(i)
End If
Next
Next
End if
It actually works well but I get errors:
System.ArgumentOutOfRangeE
Where is my problem?
This is a typical problem with VB.NET. The ending condition for the VB.NET is not re-evaluated after each iteration, it is only evaluated once, at the beginning of the For loop.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nice, it works. Thanks Arthur. I figured it was something simple.
TLO - I have been meaning to read that C# book eventually...
Thanks guys.
TLO - I have been meaning to read that C# book eventually...
Thanks guys.
Glad to be of assistance.
AW
AW