Android Question Compare Lists

magarcan

Active Member
Licensed User
Longtime User
Hi there!!

I've two lists with Strings as Objects. I've to compare which elements are in both Lits.
I've thoung in make two anidated For loops but don't know if are there a better way to do it.

Any suggestion?

Cheers!
 

stevel05

Expert
Licensed User
Longtime User
I would think the simplest would be something like:

B4X:
    Dim L1 As List = Array(1,2,3,4,8)
    Dim L2 As List = Array(3,6,7,8)
    Dim Results As List
    Results.Initialize
    
    For Each O As Object In L1
        If L2.IndexOf(O) > -1 Then Results.Add(O)
    Next

    For Each O As Object In Results
        Log(O)
    Next
 
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
Here goes my code:
B4X:
'Compare 2 lists
    For x = 0 To CountryList1.Size-1
        For y = 0 To CountryList2.Size-1
            If (CountryList1.Get(x) == CountryList2.Get(y)) Then
                Log(CountryList1.Get(x) & " are in common")
            End If
        Next
    Next
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I've two lists with Strings as Objects. I've to compare which elements are in both Lits.
1) You cannot compare Strings and Objects

2) You could not compare two objects, you should compare their content (unless you want to know if they are the same object or, better, the same reference to the same object)

3) This is not B4:



For x = 0 To CountryList1.Size-1
For y = 0 To CountryList2.Size-1
if both CountryList1 and CountryList2 are lists of strings:
B4X:
For Each Country As String In CountryList1
    If CountryList2.IndexOf(Country) <> - 1 Then
        Log(Country & " is also in CountryList2")
    End If
Next
 
Upvote 0
Top