Link to home
Start Free TrialLog in
Avatar of daJman
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.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.ArrayList.get_Item(Int32 index) ...

Where is my problem?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of daJman
daJman

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.
Glad to be of assistance.

AW