Android Question Clear list, unexpected result? [Solved]

Paul_

Member
Licensed User
Sample code:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    
    Private listA As List
    Private listB As List

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    'MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    'MainForm.Show

    listA.Initialize
    listB.Initialize
    
    listA.AddAll(Array As String("item1", "item2"))
    
    listB=listA
    
    Log(listA)
    Log(listB)
    
    listB.Clear
    
    Log(listA)
    Log(listB)
    
    'both lists are now empty, why is listA cleared?

End Sub

Same result with B4J 9.30 and B4A 11.50

Why should listA be cleared?
 

Paul_

Member
Licensed User
now listB points to the same object (listA), so there are no bugs.
But they have been created as two separate lists like two separate variables. If I create two Ints, copy one value to the other and then clear one the other is not cleared that would cause massive issues. Are you saying this is how Java works with lists?
 
Upvote 0

Paul_

Member
Licensed User
If you write:
B4X:
listB.AddAll(listA)
The contents of listA is added to the contents of listB.
The clear method removes all the items from the list (listB)
So now listA remains the same!
That code will just Add all of the items from ListA to the current ListB collection.

My argument is that if you create multiple lists and simply make one list=to another list, clearing either list should not clear the other list. But that is what happens. There are many ways around this issue but I'm just puzzled as to why or if it should happen?

I understand LucaMS explanation but just because ListB now links to the same 'object' as ListA, if you clear ListB it should clear the 'linking' but not have an effect on ListA's contents (in my mind anyway).
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I understand LucaMS explanation but just because ListB now links to the same 'object' as ListA, if you clear ListB it should clear the 'linking' but not have an effect on ListA's contents (in my mind anyway).

No, listA and listB will be two variables pointing to the same object, so whatever property or method you use of one of the two will act on the other as well.

To delete the reference you need to set listB to Null (then to reuse it, you should initialize it again)
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
but I'm just puzzled as to why or if it should happen?

There are two sorts of objects, primitives, like Ints, Doubles, Booleans etc, and object instances. Variables can be regarded as a memory address that stores a value. For variables declared as primitives the value stored is that of the primitive. For variables declared as instances the value is a pointer to the instance.

Assignment from one variable to another copies the value in one variables' memory address to the memory address of the other. So copying an instance value results in both variables pointing to the same object.

Similarly when passing values to parameters of Subs. Values are 'passed by value' in Java so a primitive is passed as a value and an instance is passed as a pointer to the instance.
 
Upvote 0

Paul_

Member
Licensed User
Thank you for all of the replies and the explanation. An interesting conversation. I hope it helps others in the excellent B4X community :)
 
Upvote 0
Top