NevSoFly
asked on
Need help comparing 2 arraylists.
Hi, I am looking for a code sample to help with the following situation.
Widget - Custom class with a color property.
DoSomething - Function that takes 2 widgets as arguments and returns a Widget.
I need to compare two arraylists against each other element by element. Both arraylists consist of Widgets with a string "+" in between each Widget and they (the arraylists) may be of different lengths. For example:
When comparing the arraylists:
This should cause all of the Widgets in ArrLst1 to be added to SumArr. If there are any leftover Widgets in ArrLst2 after the comparison has been completed they should be added to the end of SumArr with a string "+" in between each Widget. Using the previous examples for ArrLst1 and ArrLst2, The final contents of SumArr would be {[a,a],+,b,+,[c,c],+,d,+,e }.
Here is the code that I have been trying to get to work:
Once I complete my final solution I do intend to change from arraylists to something else, so if you have any thoughts on what would be the best collection to use I'm all ears.
Widget - Custom class with a color property.
DoSomething - Function that takes 2 widgets as arguments and returns a Widget.
I need to compare two arraylists against each other element by element. Both arraylists consist of Widgets with a string "+" in between each Widget and they (the arraylists) may be of different lengths. For example:
• ArrLst1 would contain {a+b+c}
• ArrLst2 would contain {a+c+d+e}
The results of the comparison will be held in a third arraylist called SumArr.When comparing the arraylists:
• If the elements being compared are both Widgets and:
o They have the same color value. Perform the DoSomething function on both Widgets and add the returned widget to SumArr. For display purposes the returned Widget will be represented as [ArrLst1(element),ArrLst2( element)].
o They have different color values. Add the widget from ArrLst1 to SumArr.
• After a widget has been added to SumArr:
o Automatically add a “+” to SumArr.
o That widget can no longer be used in future comparisons (in either ArrLst1 or ArrLst2). This is where my problem really happens. I cannot find a way to not include, skip over or remove the elements from the arraylists after I have used them and not cause an error for changing the arraylist.
Here is the code that I have been trying to get to work:
Sub Main()
Dim ArrLst1 As New ArrayList
Dim ArrLst2 As New ArrayList
Dim SumArr As New ArrayList
Dim ExitA As Boolean
Dim T1 As Integer
Dim T2 As Integer
ArrLst1.AddRange("a,+,b,+,c".Split(","))
ArrLst2.AddRange("a,+,c,+,d,+,e".Split(","))
While ArrLst1.Count > 0 And ExitA = False
While T2 <= ArrLst2.Count - 1 And ExitA = False
If ArrLst1(T1).Equals(ArrLst2(T2)) Then
If ArrLst1(T1) <> "+" Then
SumArr.Add(DoSomething(ArrLst1(T1), ArrLst2(T2)))
Else
SumArr.Add(ArrLst1(T1))
End If
ArrLst1.Remove(ArrLst1(T1))
ArrLst2.Remove(ArrLst1(T2))
ExitA = True
Else
SumArr.Add(ArrLst1(T1))
ArrLst1.Remove(ArrLst1(T1))
End If
T2 += 1
End While
T1 += 1
ExitA = False
End While
SumArr.AddRange(ArrLst2)
For Each item As String In SumArr
Console.Write(item)
Next
Console.ReadLine()
Once I complete my final solution I do intend to change from arraylists to something else, so if you have any thoughts on what would be the best collection to use I'm all ears.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.