pliroforikos Active Member Licensed User Mar 14, 2021 #1 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. 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? Attachments lesson15-L4.png 22 KB · Views: 677 lesson15-L1.png 10.1 KB · Views: 635 lesson15-L2.png 14.8 KB · Views: 658 lesson15-L3.png 15.2 KB · Views: 586 lesson15-L5.png 27.2 KB · Views: 681 lesson15-L2.png 14.9 KB · Views: 646 lesson15-L3.png 15.7 KB · Views: 600 lesson15-L4.png 21.8 KB · Views: 629 Last edited: Mar 14, 2021
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. 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?
ilan Expert Licensed User Longtime User Mar 14, 2021 #2 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
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 Mar 14, 2021 #3 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
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 Mar 14, 2021 #4 ilan said: 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 Click to expand... So the arr Array lost after entered to the list?
ilan said: 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 Click to expand... So the arr Array lost after entered to the list?
ilan Expert Licensed User Longtime User Mar 14, 2021 #5 pliroforikos said: I don't understand the next code where he declares the same array inside for loop Click to expand... it is not the same array it is a new array. isinde the LOOP you are declaring a new array with: B4X: Dim arr(3) As Int
pliroforikos said: I don't understand the next code where he declares the same array inside for loop Click to expand... it is not the same array it is a new array. isinde the LOOP you are declaring a new array with: B4X: Dim arr(3) As Int
ilan Expert Licensed User Longtime User Mar 14, 2021 #6 pliroforikos said: So the arr Array lost after entered to the list? Click to expand... 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 Click to expand...
pliroforikos said: So the arr Array lost after entered to the list? Click to expand... 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 Click to expand...
ilan Expert Licensed User Longtime User Mar 14, 2021 #7 define what you want to get and i will write the code
pliroforikos Active Member Licensed User Mar 14, 2021 #8 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?
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?
ilan Expert Licensed User Longtime User Mar 14, 2021 #9 pliroforikos said: I know that lists doesn't store any value only references. Click to expand... 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] Click to expand... 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.
pliroforikos said: I know that lists doesn't store any value only references. Click to expand... 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] Click to expand... 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 Mar 14, 2021 #10 pliroforikos said: o how list reference to 3 arrays with same array name? Click to expand... The variable name is not important. Code: 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.
pliroforikos said: o how list reference to 3 arrays with same array name? Click to expand... The variable name is not important. Code: 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.
pliroforikos Active Member Licensed User Mar 14, 2021 #11 ok, now i understand I misunderstand the list values. Now all explained! Thank you
pliroforikos Active Member Licensed User Mar 14, 2021 #12 So that happens? and if yes why 1st code at my first post doesn't work?
Erel B4X founder Staff member Licensed User Longtime User Mar 14, 2021 #13 Code: 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: Code: 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
Code: 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: Code: 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 Mar 14, 2021 #14 This is easy cos the different names of variables The picture above means that list store values inside it not references .
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 Mar 14, 2021 #15 pliroforikos said: This is easy cos the different names of variables Click to expand... 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. pliroforikos said: The picture above means that list store values inside it not references . Click to expand... The list stores references to arrays.
pliroforikos said: This is easy cos the different names of variables Click to expand... 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. pliroforikos said: The picture above means that list store values inside it not references . Click to expand... The list stores references to arrays.
pliroforikos Active Member Licensed User Mar 14, 2021 #16 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?
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 Mar 14, 2021 #17 Perhaps this would be easier if we updated the example to use other variable names..?
pliroforikos Active Member Licensed User Mar 14, 2021 #18 Sandman said: Perhaps this would be easier if we updated the example to use other variable names..? Click to expand... 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
Sandman said: Perhaps this would be easier if we updated the example to use other variable names..? Click to expand... 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 Mar 14, 2021 #20 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). Code: 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: Mar 14, 2021
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). Code: 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