Easy declaration of a map...

kcorey

Member
Licensed User
Longtime User
I saw I could declare an array like this:

dim arrName() as int
arrName = array as int(1,2,3,4,5,6,7,8,9)

Is there a way to do this with map as well, or must I do this:

dim mapName as Map
mapname.Initialize()
mapName.put("key1","value")
mapName.put("key2","value")
mapName.put("key3","value")
...

It surely seems long-winded.

It'd be great to be able to say:
dim mapName as Map("key1":"value","key2":"value","key3":"value")

-Ken
 

mc73

Well-Known Member
Licensed User
Longtime User
I don't know but you can simply generate an array, loop through its contents populating the map:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here is the implementation:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim colorsMap As Map
   colorsMap = A2M(Array As Object("red", Colors.Red, "blue", Colors.Blue, "black", Colors.Black))
   Log(colorsMap)
End Sub

Sub A2M (arr() As Object) As Map
   Dim m As Map
   m.Initialize
   For i = 0 To arr.Length - 1 Step 2
      m.Put(arr(i), arr(i + 1))
   Next
   Return m
End Sub
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
How does the internal structure of a map work? When parsing through it by index it is in the order they were added which would indicate an array type structure possibly similar to the original post. A way to pass an array or cast to it would be interesting. Is map a standard Android/JAVA type, and does it use any kind of an indexing system?
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Does that mean it is a one-directional linked list and internally it just stores a reference to the last accessed object in the list? So, if you access items 1-100 it keeps a reference to item 100 and if you access 101 it is quick, but then accessing 50 it has to go back through 1-49?

Also, would this apply to reading by key as well? So, if you read by key and it happens to be index 100 then you read index 101 or the key to index 101 would it read it quicker?
 
Last edited:
Top