B4J Version: 7.51
OS Platfrom: Win10 Pro (1903)
JDK: 11.0.1 downloaded from B4X's site
Here's the setup. I'm creating a list of maps that contain x, y values (note: in my case, the map contains way more info, I'm just simplifying it here to show the issue I'm running into)
Output:
And the ouput is:
Output:
And why does this modified version produce a correct output if an intermediate list is used?
Output:
OS Platfrom: Win10 Pro (1903)
JDK: 11.0.1 downloaded from B4X's site
Here's the setup. I'm creating a list of maps that contain x, y values (note: in my case, the map contains way more info, I'm just simplifying it here to show the issue I'm running into)
B4X:
Dim largestX As Int = 4
Dim mapList As List
mapList.Initialize
For x = 0 To largestX
Dim aMap As Map
aMap.Initialize
aMap.Put("x", x)
aMap.Put("y", x+1)
mapList.Add(aMap)
Next
Log("Dump of mapList:")
For Each tempMap As Map In mapList
Log($"(x,y) = (${tempMap.Get("x")},${tempMap.Get("y")})"$)
Next
Now I'm going to try to find the map that contains the smallest x amount:Dump of mapList:
(x,y) = (0,1)
(x,y) = (1,2)
(x,y) = (2,3)
(x,y) = (3,4)
(x,y) = (4,5)
B4X:
Log("Let's find the smallest x value. It should be 0")
Dim smallestXmap As Map
Dim smallestX As Int = largestX
For Each temp2Map As Map In mapList
If temp2Map.Get("x") < smallestX Then
Log("Found a smaller x!")
smallestX = temp2Map.Get("x")
smallestXmap = temp2Map
End If
Next
Log($"Smallest x value is ${smallestX}"$)
Log("The log entry below should show content of (0,1), but it does not. Why?")
Log($"Map containing smallest x value has following content: (${smallestXmap.Get("x")},${smallestXmap.Get("y")})"$)
Why? Why are the contents of smallestXmap 4,5? If temp2Map is just a reference and stays the same, then why does the following code work?Let's find the smallest x value. It should be 0
Found a smaller x!
Smallest x value is 0
The log entry below should show content of (0,1), but it does not. Why?
Map containing smallest x value has following content: (4,5)
B4X:
'https://www.b4x.com/android/forum/threads/maps-for-each-but-backwarts-like-step-1.64131/#post-405821
Log("The list below is produced properly. What is the difference (besides code)?")
Dim itemsToRemove As List
itemsToRemove.Initialize
For Each temp3Map As Map In mapList
If temp3Map.Get("x") > 0 And temp3Map.Get("x") < largestX Then
itemsToRemove.Add(temp3Map)
End If
Next
For Each temp4Map As Map In itemsToRemove
Log($"Map with content (${temp4Map.Get("x")}, ${temp4Map.Get("y")}) flagged for removal"$)
Next
Would I not be adding the same temp3Map reference to the list and get (3,4) for all three logs?The list below is produced properly. What is the difference (besides code)?
Map with content (1, 2) flagged for removal
Map with content (2, 3) flagged for removal
Map with content (3, 4) flagged for removal
And why does this modified version produce a correct output if an intermediate list is used?
B4X:
Log("Let's find the smallest x value, try#2. It should be 0")
Dim smallestXmap2 As Map
Dim smallestX2 As Int = largestX
Dim smallestXList As List
smallestXList.Initialize
smallestXList.Add("Filler")
smallestXList.Add("Filler")
For Each temp5Map As Map In mapList
If temp5Map.Get("x") < smallestX2 Then
Log("Found a smaller x!")
smallestX2 = temp5Map.Get("x")
smallestXmap2 = temp5Map
smallestXList.Set(0,temp5Map)
'Just for kicks
smallestXList.Set(1,smallestXmap2)
End If
Next
Log($"Smallest x value is ${smallestX2}"$)
Log("The log entry below should show content of (0,1), but it does not. Why?")
Log($"Map containing smallest x value has following content: (${smallestXmap2.Get("x")},${smallestXmap2.Get("y")})"$)
Log("Extracting map from smallestXList list")
Dim lastTempMap As Map
lastTempMap = smallestXList.Get(0)
Log($"Map containing smallest x value has following content: (${lastTempMap.Get("x")},${lastTempMap.Get("y")})"$)
Log("Why did this work?")
Dim justForFun As Map
justForFun = smallestXList.Get(1)
Log("Retrieving smallestXmap2 from list")
Log($"Map containing smallest x value has following content: (${justForFun.Get("x")},${justForFun.Get("y")})"$)
Log("This works too!!! I'm confused (Note: It should, but then why does the previous smallestXmap2 log produce it's result?).")
What am I not seeing/understanding? I've lost over 1/2 of a day on this and it's bugging me.Let's find the smallest x value, try#2. It should be 0
Found a smaller x!
Smallest x value is 0
The log entry below should show content of (0,1), but it does not. Why?
Map containing smallest x value has following content: (4,5)
Extracting map from smallestXList list
Map containing smallest x value has following content: (0,1)
Why did this work?
Retrieving smallestXmap2 from list
Map containing smallest x value has following content: (0,1)
This works too!!! I'm confused (Note: It should, but then why does the previous smallestXmap2 log produce it's result?).
Program terminated (StartMessageLoop was not called).