A atiaust Active Member Licensed User Longtime User Mar 18, 2017 #1 I am having trouble using a map to store some data while reading a mysql data base table. There appears to be a bug while writing data to the map using map.put. I have 27 records in a table. In a loop I read each record and add an Int value stored at position 4 in the record to the map. Last part of the record read loop: Code: Copy ' vehicle capacity map - key Is capacity, Value = Vehicle ComboBox Index capacity = cursor.Getint2(4) capacityMap.Put(capacity,j) Log("Capacity = "&cursor.GetInt2(4)) j = j + 1 Loop cursor.Close Spoiler: Log Result Capacity = 13 Capacity = 8 Capacity = 5 Capacity = 13 Capacity = 13 Capacity = 11 Capacity = 21 Capacity = 7 Capacity = 11 Capacity = 11 Capacity = 25 Capacity = 11 Capacity = 11 Capacity = 11 Capacity = 11 Capacity = 11 Capacity = 13 Capacity = 11 Capacity = 13 Capacity = 11 Capacity = 13 Capacity = 25 Capacity = 22 Capacity = 14 Capacity = 4 Capacity = 12 Capacity = 13 To check the contents of the map Code: Copy For i = 1 To capacityMap.Size - 1 LogDebug("Capacity = "&capacityMap.Get(i)) Next Spoiler: Map Contents Capacity = null Capacity = null Capacity = null Capacity = 24 Capacity = 2 Capacity = null Capacity = 7 Capacity = 1 Capacity = null Capacity = null As above only 12 items are added to the map and all vales are incorrect. If I swap capacityMap.Put(capacity,j) to read capacityMap.Put(j,capacity) then the values stored in the map are correct with 27 items in the map.
I am having trouble using a map to store some data while reading a mysql data base table. There appears to be a bug while writing data to the map using map.put. I have 27 records in a table. In a loop I read each record and add an Int value stored at position 4 in the record to the map. Last part of the record read loop: Code: Copy ' vehicle capacity map - key Is capacity, Value = Vehicle ComboBox Index capacity = cursor.Getint2(4) capacityMap.Put(capacity,j) Log("Capacity = "&cursor.GetInt2(4)) j = j + 1 Loop cursor.Close Spoiler: Log Result Capacity = 13 Capacity = 8 Capacity = 5 Capacity = 13 Capacity = 13 Capacity = 11 Capacity = 21 Capacity = 7 Capacity = 11 Capacity = 11 Capacity = 25 Capacity = 11 Capacity = 11 Capacity = 11 Capacity = 11 Capacity = 11 Capacity = 13 Capacity = 11 Capacity = 13 Capacity = 11 Capacity = 13 Capacity = 25 Capacity = 22 Capacity = 14 Capacity = 4 Capacity = 12 Capacity = 13 To check the contents of the map Code: Copy For i = 1 To capacityMap.Size - 1 LogDebug("Capacity = "&capacityMap.Get(i)) Next Spoiler: Map Contents Capacity = null Capacity = null Capacity = null Capacity = 24 Capacity = 2 Capacity = null Capacity = 7 Capacity = 1 Capacity = null Capacity = null As above only 12 items are added to the map and all vales are incorrect. If I swap capacityMap.Put(capacity,j) to read capacityMap.Put(j,capacity) then the values stored in the map are correct with 27 items in the map.
Erel B4X founder Staff member Licensed User Longtime User Mar 19, 2017 #2 There is no bug here. Keys must be unique. See this tutorial: https://www.b4x.com/android/forum/threads/map-collection-the-most-useful-collection.60304/ Upvote 0
There is no bug here. Keys must be unique. See this tutorial: https://www.b4x.com/android/forum/threads/map-collection-the-most-useful-collection.60304/
A atiaust Active Member Licensed User Longtime User Mar 19, 2017 #3 Thanks Erel. I had forgten that the key needs to be unique. Sbould remember from now on. Upvote 0