Android Question If List1 = List2 doesn't work

Tim Chapman

Active Member
Licensed User
Longtime User
I want to quickly compare two lists to see if they contain the same information.
Why does this code say the lists are equal when they are not?
The log shows List1 = List2.

List1=List2:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    Dim List1 As List
    List1.Initialize
    Dim List2 As List
    List2.Initialize
   
    List1.AddAll(Array As String("value1", "value2"))
    List2.AddAll(Array As String(""))
   
    If List1 = List2 Then
        Log("List1 = List2")
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

Star-Dust

Expert
Licensed User
Longtime User
Lists are complex objects and are managed by reference, not by value. You're comparing references, which will never be correct. Loop through the list and compare the individual values. But I'd start by comparing the length of the lists.
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
The answer doesn't change, the comparison will never give a correct result. The content values must be compared with loop.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The answer doesn't change, the comparison will never give a correct result. The content values must be compared with loop.
The question is: why is the log "List1 = List2" written even though they are two different objects (and with different content, but that's not the point)

Il log non dovrebbe avvenire, dato IF List1 = List2 Then Log... (ma non per il contenuto, per i puntatori diversi, ovviamente).
 
Last edited:
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Could something like this work correctly?
B4X:
Dim List1 As List
Dim List2 As List
Dim List3 As List
List1.Initialize
List2.Initialize
List3.Initialize
List1.AddAll(Array As String("One", "Two", "Three", "Four"))
List2.AddAll(Array As String("Four", "Three", "Two", "One"))
List3.AddAll(Array As String("One", "Two", "Three", "Four"))
Dim s As B4XSerializator
Dim bc As ByteConverter
Dim b1 As String = bc.StringFromBytes(s.ConvertObjectToBytes(List1), "UTF8")
Dim b2 As String = bc.StringFromBytes(s.ConvertObjectToBytes(List2), "UTF8")
Dim b3 As String = bc.StringFromBytes(s.ConvertObjectToBytes(List3), "UTF8")
If b1 = b3 Then
    Log("List1 is equal to List3")
End If
If b1 = b2 Then
Else
    Log("List1 is NOT equal to List2")
End If
Tested in B4J.
Flagged libraries:
RandomAccessFile
ByteConverter
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Tried (B4A): no log!
No log because it is false.

B4X:
If List1 = List2 Then
    Log("List1 = List2")
Else
    Log("List1 <> List2")
End If
I tried in B4A and B4J, it shows "List1 <> List2"
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Maybe a sub that compares initially the lengths and if they are both the same, then sort both lists and compare each individual value in X position in both lists, would do the trick.
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
The log always shows when I run it. I don't know why some are seeing otherwise.
I converted the relevant contents of the lists to one string for the entire list, then compared the strings to see if they are equal. This is a definitive test to see if they same.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
You may also try:
B4X:
If List1.As(JSON) = List2.As(JSON) Then
    Log("List1 is equal to List2")
Else
    Log("List1 is not equal to List2")
End If
 
Upvote 0
Top