Explaining Lists

pliroforikos

Active Member
Licensed User
Good day to all,
In this thread Erel explaining how to assign Arrays in List Items.
In the first code writes
B4X:
Dim arr(3) As Int
Dim List1 As List
List1.Initialize
For i = 1 To 5
arr(0) = i * 2
arr(1) = i * 2
arr(2) = i * 2
List1.Add(arr) 'Add the whole array as a single item
Next
arr = List1.Get(0) 'get the first item from the list
Log(arr(0)) 'What will be printed here???

It is something like this and i understand why this is happens.
lesson15-L1.png
lesson15-L2.png


lesson15-L3.png

lesson15-L4.png



I don't understand the next code where he declares the same array inside for loop

B4X:
Dim arr(3) As Int 'This call is redundant in this case.
Dim List1 As List
List1.Initialize
For i = 1 To 5
Dim arr(3) As Int
arr(0) = i * 2
arr(1) = i * 2
arr(2) = i * 2
List1.Add(arr) 'Add the whole array as a single item
Next
arr = List1.Get(0) 'get the first item from the list
Log(arr(0)) 'Will print 2

Why the previous arrays is not lost? If i log(Arr(0)) after for loop what are we gonna see?

lesson15-L5.png
 

Attachments

  • lesson15-L4.png
    lesson15-L4.png
    22 KB · Views: 242
  • lesson15-L1.png
    lesson15-L1.png
    10.1 KB · Views: 253
  • lesson15-L2.png
    lesson15-L2.png
    14.8 KB · Views: 246
  • lesson15-L3.png
    lesson15-L3.png
    15.2 KB · Views: 243
  • lesson15-L5.png
    lesson15-L5.png
    27.2 KB · Views: 248
  • lesson15-L2.png
    lesson15-L2.png
    14.9 KB · Views: 247
  • lesson15-L3.png
    lesson15-L3.png
    15.7 KB · Views: 244
  • lesson15-L4.png
    lesson15-L4.png
    21.8 KB · Views: 244
Last edited:

ilan

Expert
Licensed User
Longtime User
B4X:
For i = 1 To 5
Dim arr(3) As Int
arr(0) = i * 2
arr(1) = i * 2
arr(2) = i * 2
List1.Add(arr) 'Add the whole array as a single item
Next

you are adding on each cycle an array to the list.
if you want to set the array inside a loop then set it and after loop cycle is finish add the array to the list.

B4X:
Dim arr(5) As Int
Dim List1 As List
List1.Initialize

For i = 0 To 4
    arr(i) = i * 2
Next

List1.Add(arr) 'Add the whole array as a single item

dim newarr() as int = List1.Get(0) 'get the first item from the list
Log(newarr(0)) '0
 

ilan

Expert
Licensed User
Longtime User
or maybe i misunderstood you, what exactly do you want to do?

you can do it also like this if you want to add on each cycle a new array to the list

B4X:
    Dim arr(3) As Int 'This call is redundant in this case.
    Dim List1 As List
    List1.Initialize
    For i = 1 To 5
        Dim arr(3) As Int
        arr(0) = i * 2
        arr(1) = i * 2
        arr(2) = i * 2
        List1.Add(arr) 'Add the whole array as a single item
    Next
    Dim newarr() As Int = List1.Get(0) 'get the first item from the list
    Log(newarr(0)) 'Will print 2
 

pliroforikos

Active Member
Licensed User
B4X:
For i = 1 To 5
Dim arr(3) As Int
arr(0) = i * 2
arr(1) = i * 2
arr(2) = i * 2
List1.Add(arr) 'Add the whole array as a single item
Next

you are adding on each cycle an array to the list.
if you want to set the array inside a loop then set it and after loop cycle is finish add the array to the list.

B4X:
Dim arr(5) As Int
Dim List1 As List
List1.Initialize

For i = 0 To 4
    arr(i) = i * 2
Next

List1.Add(arr) 'Add the whole array as a single item

dim newarr() as int = List1.Get(0) 'get the first item from the list
Log(newarr(0)) '0
So the arr Array lost after entered to the list?
 

ilan

Expert
Licensed User
Longtime User
So the arr Array lost after entered to the list?

no you can access all array if you want to:

B4X:
    Dim arr(3) As Int 'This call is redundant in this case.
    Dim List1 As List
    List1.Initialize
    For i = 1 To 5
        Dim arr(3) As Int
        arr(0) = i * 2
        arr(1) = i * 2
        arr(2) = i * 2
        List1.Add(arr) 'Add the whole array as a single item
    Next
    Dim arr() As Int = List1.Get(3) 'get the first item from the list
    Log(arr(0)) 'Will print 8

this will print 8

Waiting for debugger to connect...
Program started.
8
 

pliroforikos

Active Member
Licensed User
Well i don't want to create a certain code but to explain what happens with lists.

I know that lists doesn't store any value only references. So how list reference to 3 arrays with same array name? Or maybe array name is lost but data still remain in memory?
:eek:
 

ilan

Expert
Licensed User
Longtime User
I know that lists doesn't store any value only references.

as far as i know this is not true. you create an object and put it inside a list that holds this object. now you can refer to the object that is holded in the list.

a list is not just pointing to objects that are created somewhere in the class. if it was like this then this code would never work:

B4X:
    Dim l As List
    l.Initialize
    
    For i = 0 To 9
        Dim txt As String = "item" & i
        l.Add(txt)
    Next

    Log(l)

Waiting for debugger to connect...
Program started.
(ArrayList) [item0, item1, item2, item3, item4, item5, item6, item7, item8, item9]

as you can see i am declaring a new variable (string) and add it to a list with a value. now all strings are stored inside the list.

if the list was declared as a global variable i could access it from anywhere in the class so it is storing data inside and not just storing reference to some objects.
it is storing the objects themselves.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
o how list reference to 3 arrays with same array name?
The variable name is not important.

B4X:
For i = 1 To 5
 Dim arr(3) As Int
 arr(0) = i * 2
 arr(1) = i * 2
 arr(2) = i * 2
 List1.Add(arr) 'Add the whole array as a single item
Next
A new array is created each iteration.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim arr(3) As Int
This important line creates a new array of ints with 3 slots and assigns it to a local variable name arr.

I'm not sure that I understand the illustration.

Another example:
B4X:
Dim arr() As Int = Array As Int(1, 2, 3)
Log(arr(0)) '1

Dim arr2() As Int = arr
arr2(0) = 100
Log(arr(0)) '100

Dim arr(3) As Int
arr(0) = 200
Log(arr(0)) '200
Log(arr2(0)) '100
 

pliroforikos

Active Member
Licensed User
This is easy cos the different names of variables
The picture above means that list store values inside it not references .
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is easy cos the different names of variables
No. Two arrays are created in this example.
arr points to the first one and then to the second.
arr2 points to the first one.


The picture above means that list store values inside it not references .
The list stores references to arrays.
 

pliroforikos

Active Member
Licensed User
Well, forgive me but i'm still confused.
So lists stores references to values and not the values and its not important the variable name of value... ?
Or it has to do with primitives and non-primitives types. Primitives types stored by value and non-primitives by reference?
 

Sandman

Expert
Licensed User
Longtime User
Perhaps this would be easier if we updated the example to use other variable names..?
 

pliroforikos

Active Member
Licensed User
Perhaps this would be easier if we updated the example to use other variable names..?
No it's not useful to declare more variables. But ok, i think i got the idea and now i can explain it
to my students... I hope ?
Thank you all
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'll give another example. It is logically correct (under the hood there is special treatment for primitives but it is only done for performance reasons).
B4X:
Dim x As Int = 3 'x points to an int object with the value 3. This is an immutable object. All ints are immutable objects.
x = 4 'now x points to a different int object.
Dim y As Int = x 'y points to the same object that x points to. y is not a reference or alias of x. A copy of the pointer is made.
x = 5 'y still points to 4
Dim s1 As String = "abc" 's1 points to the immutable string object with the value "abc"
Dim s2 As String = s1 's2 was assigned with a copy of the pointer to "abc"
s1 = s1 & "d" 's1 points to a new immutable string object
Log(s2) 'abc
Dim sb As StringBuilder 'sb points to a StringBuilder object. StringBuilder is a mutable object, this means that the object state can be changed.
sb.Initialize
sb.Append("abc") 'sb still points to the same object. The only way to make sb point to a different object is with the '=' operator or by calling Dim.
Dim sb2 As StringBuilder = sb 'a copy of the pointer to the single StringBuilder was made.
sb2.Appean("d")
Log(sb.ToString) 'abcd
Log(sb2.ToString) 'abcd
 
Last edited:
Top